How would you describe this bug? Is this a SwiftUI or Swift bug?
My minimum sample code demonstrating the problems:
import SwiftUI
// this is the baseline version and see how thing can change by simply re-arraging init order or initialization of `b`
struct ContentViewCase1: View {
@State var a: Int?
@State var b: Optional<Int> // ! b/c this is not initialized,
init() {
a = 1234
b = 4444
}
var body: some View {
VStack {
Text("a = \(a.map({ $0.formatted() }) ?? "nil")")
Text("b = \(b.map({ $0.formatted() }) ?? "nil")")
}
}
}
// the following two views should be the same as above but is not
// how do you describe this bug to apple for filing a feedback to SwiftUI
// or is this a Swift lang bug?
struct ContentViewCase2: View {
@State var a: Int?
@State var b: Optional<Int> // ! b/c this is not initialized,
init() {
b = 4444 // ! just reverse the order and we get another result
a = 1234
}
var body: some View {
VStack {
Text("a = \(a.map({ $0.formatted() }) ?? "nil")")
Text("b = \(b.map({ $0.formatted() }) ?? "nil")")
}
}
}
struct ContentViewCase3: View {
@State var a: Int?
@State var b: Optional<Int> = 9999 // by assigning something here
init() {
a = 1234 // a can no longer be initialized because due to b being init'ed, it get init'ed, unlike case 1
b = 4444
}
var body: some View {
VStack {
Text("a = \(a.map({ $0.formatted() }) ?? "nil")")
Text("b = \(b.map({ $0.formatted() }) ?? "nil")")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentViewCase1()
ContentViewCase2()
ContentViewCase3()
}
}