As Natascha notes in her helpful article
https://tanaschita.com/20230807-migrating-to-observation/
pre-iOS17 was like this,
Stateview Subview
-----------------------------------------
Value Type @State @Binding
Ref Type @StateObject @ObservedObject
With iOS 17, it's like this:
Stateview Subview
-----------------------------------------
Value Type @State @Binding
Ref Type @State @Bindable
I like how they simplified @State and @StateObject into just @State for both cases. I'm curious, though, why didn't they simplify @Binding and @ObservedObject into just @Binding? Why did they need to maintain the separate property wrapper @Bindable? I'm sure there's a good reason, just wondering if anybody knew why.
Interestingly, you can use both @Binding and @Bindable, and they both seem to work. I know that you're supposed to use @Bindable here, but curious why @Binding works also.
import SwiftUI
@Observable
class TestClass {
var myNum: Int = 0
}
struct ContentView: View {
@State var testClass1 = TestClass()
@State var testClass2 = TestClass()
var body: some View {
VStack {
Text("testClass1: \(testClass1.myNum)")
Text("testClass2: \(testClass2.myNum)")
// Note the passing of testClass2 without $. Xcode complains otherwise.
ButtonView(testClass1: $testClass1, testClass2: testClass2)
}
.padding()
}
}
struct ButtonView: View {
@Binding var testClass1:TestClass
@Bindable var testClass2:TestClass
var body: some View {
Button(action: {
testClass1.myNum += 1
testClass2.myNum += 2
} , label: {
Text("Increment me")
})
}
}