Still another question here
struct ContentView: View {
@State var a: Int?
init() {
a = 5
}
var body: some View {
VStack {
Text("\(a ?? -1)") // Show -1
}
}
}
struct ContentView: View {
@State var a: Int?
@State var b: Optional<Int>
init() {
a = 5
b = 5
}
var body: some View {
VStack {
Text("\(a ?? -1)") // Show 5 (unexpected, why?)
Text("\(b ?? -1)") // Show 5 (expected)
}
}
}
If we use debugger and disassemble it, we'll find that variable _a
got init twice.
1. `@State var a: Int?` // _a = State<Int?>(wrappedValue: nil)
2. `a = 5` in init funcation // _a = State<Int?>(wrappedValue: 5) // Unexpected