I am surprised and don t really understand the behavior I am seeing with the new @Observable framework in iOS 17.
看来,如果对包含“国与国”的观点加以修改,那么这种观点的内涵也将被称作“摩德尔”。 这并不是使用观点的先前国家目标方法的行为。
例:
struct ContentView: View {
@State var offset = false
var body: some View {
VStack {
InnerView()
.offset(x: offset ? 200 : 0)
Button("Offset") {
offset.toggle()
}
}
}
}
// iOS 17 Observable method:
struct InnerView: View {
@State var viewModel: ViewModel
init() {
self._viewModel = State(wrappedValue: ViewModel())
}
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
@Observable
class ViewModel {
init() {
print("ViewModel Init")
}
}
// StateObject method:
struct InnerView: View {
@StateObject var viewModel: ViewModel
init() {
self._viewModel = StateObject(wrappedValue: ViewModel())
}
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
class ViewModel: ObservableObject {
init() {
print("ViewModel Init")
}
}
这一预期行为吗?