我正在try 通过以下代码使用@Observable和@Environment来使用新的SWIFT宏:
import SwiftUI
import Observation
@Observable class NoteViewModel {
var text = ""
}
struct ContentView: View {
@State var note = NoteViewModel()
var body: some View {
FormView()
.environment(note)
}
}
struct FormView: View {
@Environment(NoteViewModel.self) var note
var body: some View {
Form {
TextField("write here", text: $note.text)
}
}
}
但是,它无法构建,并出现以下错误:
Cannot find '$note' in scope
个
删除$note
中的$
将导致:
Cannot convert value of type 'String' to expected argument type 'Binding<String>'
个