Skip to content

[SR-13896] Confusing error message when assigning an optional UIImage as a state property #57

Open
@swift-ci

Description

@swift-ci
Previous ID SR-13896
Radar rdar://60600911
Original Reporter frostra1n (JIRA User)
Type Bug

Attachment: Download

Environment

macOS Catalina 10.15.7

Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)

Xcode 12.1

Additional Detail from JIRA
Votes 0
Component/s Compiler, Xcode Playground Support
Labels Bug, PropertyWrappers
Assignee None
Priority Medium

md5: f20a4f1b3d227c0ef91b2fdd21421924

Issue Description:

I'm working on project that uses both UIKit and SwiftUI and I found a scenario that shows a confusing error when assigning a `UIImage?` in a struct initializer. The error is:

Variable 'self.asPreview' used before being initialized

And this is the code that produces the error:

import SwiftUI
import UIKit

struct BookView: View {
    let asPreview: Bool
    @State var image: UIImage?

    init(image: UIImage?, asPreview: Bool = false) {
        self.image = image         
        self.asPreview = asPreview
    }

    var body: some View {
         Group {}
     }
 }

The error is pointed in line 9 right under the equal sign ("self.image = image"), but actually that line assigns a different struct property, so it's a little bit confusing.

What it could be happening is that a different type of value is being assigned to the image variable, so the error message that should be shown instead could be something related of assigning an `UIImage?` to a `State<UIImage?>` variable.

The code above can be used in Xcode playground to reproduce the issue. I've attached a screenshot reproducing the bug.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions