无法将类型“CDAccount.Type”的值分配给类型 'CDAccount' SwiftUI

Cannot Assign Value of tyoe 'CDAccount.Type'to type 'CDAccount' SwiftUI

我有一个核心数据模型帐户 CDAccount 并试图将此帐户数据发送到 AccountDetail 视图。这样我就可以获得 selectedStatusIndex 值并从上一个视图中达到它的值。

我试图在 init 块中设置它的值,但无论我尝试什么,我都会收到一个帐户错误。

 struct AccountDetail: View {
    @Binding var account : CDAccount!
    @State var selectedStatusIndex: Int
    @State var status : [String]
    init(account: CDAccount) {
        self.account = CDAccount?
        self.selectedStatusIndex = Int(account.statusIndex)
        self.status = Constants.Status.status
    }
    var body: some View {
        Picker(selection: $selectedStatusIndex, label: Text("Status")) {
            ForEach(0..<status.count) {
//                Text(status[Int(account.statusIndex)])
                Text("\(self.status[[=12=]])")
            }
        }
    }
}

您将 account 声明为 CDAccount 类型的变量:

@Binding var account : CDAccount!

然后在这一行

self.account = CDAccount?

...您将 CDAccount 类型分配给该变量。该变量是 CDAccount 的一个实例。 CDAccount 本身就是代表该类型所有实例的 class。

很难说出您在这里要做什么,但是您不能将 class 分配给需要 class 实例的对象。您需要创建和实例化,或从应用中的其他位置提供一个。

更改 init 的前两行

init(account: Binding<CDAccount>) {
     self._account = account

还有,你不需要感叹

 @Binding var account : CDAccount

您还应该摆脱 @State var selectedStatusIndex,除非您打算让更改不影响 account 并且在 SwiftUI 决定重新加载 View 时重置更改。

第二种方法是避免重新加载的 Apple 方法。

@State var status : [String] = Constants.Status.status