The following code compiles without error:
import SwiftUI
struct Model {
let foo: String
// let bar: URL
}
struct ModelView: View {
@State var model: Model
init(model: Model) {
self.model = model // error here when `Model.bar` is uncommented
}
var body: some View {
Text(model.foo)
}
}
However, uncommenting the bar
property in Model
leads to an error:
Variable 'self.model' used before being initialized
in ModellView.init()
.
Why does this happen? Adding other properties to Model
does not cause this problem, e.g. changing the type of bar
to String
, Int
, CGFloat
, NSLayoutConstraint
and many others works. OTOH, Date
causes the same error as URL
.
(using Swift 5.7 in Xcode 14.1)