为什么我得到数据模型错误只有具体类型才能符合协议

Why do I get data model error only concrete types can conform to protocols

我想将我的数据模型与 ContentView 分开。所以我添加了一个包含以下代码的 SwiftUI 文件:

import SwiftUI
import Combine

class User: BindableObject {
    let willChange = PassthroughSubject<Void, Never>()
    var username : String = "Jan" { willSet { willChange.send() }}
    var password : String = "123456" { willSet { willChange.send() } }
    var emailAddress : String = "jan@mail.nl" { willSet { willChange.send() } }
}

#if DEBUG
struct User_Previews: PreviewProvider {
    static var previews: some View {
        User()
            .environmentObject(User())
    }
}
#endif

我得到的错误是:

Protocol type 'Any' cannot conform to 'View' because only concrete types can conform to protocols

.environmentObject(User()) 行发生错误。

您不需要使用 SwiftUI 文件。它是一个简单的 class,这是必需的。当然,如果您删除下面的代码,它将起作用。

#if DEBUG
struct User_Previews: PreviewProvider {
    static var previews: some View {
        User()
            .environmentObject(User())
    }
}
#endif