I have the following SwiftUI 5
code snippet using XCode 15 beta 4
:
struct ContentView: View {
var person: Person = .init(id: UUID(), name: "Stacey")
//using @State or @Bindable or nothing before 'person' does the same job
var body: some View {
VStack {
Button {
person.name = "Sarah"
} label: {
Text("change name")
}
Text("Name: \(person.name)")
}
}
}
@Observable
class Person: Identifiable {
var id: UUID
var name: String
init(id: UUID, name: String) {
self.id = id
self.name = name
}
}
I wonder what is the difference between using: @Bindable
, @State
, or nothing before the Person
variable in the ContentView
, because the person
object updates successfully using any of those 3 choices, and the view can listen to the new change.