无法将类型为"字符串"的值转换为预期的参数类型"绑定<String>"



我是SwiftUi的新手,遇到了一个无法修复的错误。基本上,我想在SettingsView中更改AB类的名称属性。

此外,我还有一些问题希望大家都能回答。

  1. 当AB类已经在我的User类中作为@Published属性时,我是否必须使其成为具有@Published特性的ObservableObject
  2. AB类应该是结构体吗?我正在使用类User作为EnvironmentObject
class User: ObservableObject {
@Published var name: String
...
@Publsihed var ab: [AB]
@Published var currentAb: AB?
internal init(name: String, ab: [AB]) {
self.name = name
self.ab = ab
self.currentAb = ab.first
}
}
class AB: ObervableObject {
@Published var name: String
...
}

我在这里得到错误是因为TextField("新名称",text:$user.currentAb.wrappedValue.name(

struct SettingsView: View {
@EnvironmentObject var user: User
var body: some View {
Form { //Error: Unable to infer complex closure return type; add explicit type to disambiguate
Section(header: Text("")) {
TextField("new name", text: $user.currentAb.wrappedValue.name) // <- Error is shown here
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
}

谢谢。

最好是分离到不同的视图中,如

var body: some View {
Form {
Section(header: Text("")) {
if user.currentAb != nil {
SomeNameView(vm: user.currentAb!)
} else {
Text("any holder view here")
}
}
}
}

和分离视图

struct SomeNameView: View {
@ObservedObject var vm: AB
var body: some View {
TextField("new name", text: $vm.name)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}

最新更新