You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
The text was updated successfully, but these errors were encountered:
@propertyWrapperclassA {
varwrappedValue: Int?
}
structS {
letb: Bool@Avara: Int?
init(a: Int?, b: Bool = false) {
self.a = a// 'self' used before all stored properties are initializedself.b = b
}
}
As far as I could see the problem is related to the fact that in this case @A is a class and the way self is implicit passed to the property wrapper in the assignment that is required to all properties being initialized.
To me, the diagnostics should be improved to make this more clear.
But as a workaround, it should be able to make it work by making sure initialized all the other members before the property wrapper
e.g.
init(image: UIImage?, asPreview: Bool = false) {
self.asPreview = asPreview// Make sure this is initialized before call property wrapperself.image = image
}
This is a known issue where out-of-line initialization via wrapped value isn't supported for class property wrappers and struct property wrappers with nonmutating setters (due to limitations of the assign_by_wrapper instruction in SIL). Here's a reproducer without SwiftUI:
@LucianoPAlmeida you're right that the error message should make it clear how to fix the issue. The right fix is to initialize the backing property wrapper directly, e.g.:
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
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:
And this is the code that produces the error:
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.
The text was updated successfully, but these errors were encountered: