无法将类型 'String' 的值转换为预期的参数类型 'Binding<String>'

Cannot convert value of type 'String' to expected argument type 'Binding<String>'

我是 SwiftUi 的新手,我遇到了一个我无法修复的错误。 基本上,我想在 SettingsView.

中更改 AB class 的名称属性

另外,我还有一些问题希望大家能够解答。

  1. 当 class AB 已经作为 @Published 属性存在于我的用户 class 中时,我是否必须将其设为具有 @Published 属性的 ObservableObject?
  2. class AB 应该是一个结构吗?我正在使用 class 用户作为 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("new name", 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())
    }
}