[英]Type '()' cannot conform to 'View'; only struct / enum / class types can conform to protocols
Hi I'm trying to make some changes in my app, but I can't because whenever I try to add a func in my View I get the following error: "Type '()' cannot conform to 'View'; only struct / enum / class types can conform to protocols "您好,我正在尝试对我的应用程序进行一些更改,但我做不到,因为每当我尝试在我的视图中添加一个函数时,我都会收到以下错误:“Type '()' cannot conform to 'View'; only struct /enum/class 类型可以符合协议”
What my func does:我的功能是做什么的:
func regiterUserAppleBlog(uid:String,completion :@escaping (Bool) -> (Void)) {
var isExist = false
COLLECTION_USRES.whereField("uid", isEqualTo: uid).getDocuments { snapshot, _ in
if (snapshot?.documents.count)! > 0 {
isExist = true
print("User exists")
}
completion(isExist)
}
}
It is looking in my console if the user's document exists.如果用户的文档存在,它会在我的控制台中查找。
and I am implementing it in that View because I want it to tell me if the doc exists or if not, if it does not exist I send it to create the doc and if it exists I send it to the MainTabView.我在那个视图中实现它,因为我想让它告诉我文档是否存在,如果不存在,我发送它来创建文档,如果存在,我将它发送到 MainTabView。
Code:代码:
struct ContentView: View {
@EnvironmentObject var viewModel: AuthViewModel
@State var registerUser: Bool = false
@State var selectedIndex = 0
var body: some View {
Group {
if viewModel.userSession == nil {
AppPreview()
} else {
regiterUserAppleBlog(uid: ACFF.UID ?? "") { (isExist) -> (Void) in
if !isExist {
print("User exists")
if let user = viewModel.currentUser {
MainTabView(user: user, selectIndex: $selectedIndex)
}
} else {
print("User missing")
}
}
}
}
}
func regiterUserAppleBlog(uid:String,completion :@escaping (Bool) -> (Void)) {
var isExist = false
COLLECTION_USRES.whereField("uid", isEqualTo: uid).getDocuments { snapshot, _ in
if (snapshot?.documents.count)! > 0 {
isExist = true
print("User exists")
}
completion(isExist)
}
}}
If someone can help me it would be very helpful, thanks.如果有人可以帮助我,那将非常有帮助,谢谢。
You can't make function calls inside SwiftUI view builders.您不能在 SwiftUI 视图构建器中进行 function 调用。 You can only do it in response to some events.您只能在响应某些事件时执行此操作。
And actually you need to made such calls inside your view model, and inside your view just display different views depending on view state实际上你需要在你的视图 model 中进行这样的调用,并且在你的视图中根据视图 state 显示不同的视图
You can place your register logic inside didSet
, so it'll be called once session is established.您可以将注册逻辑放在didSet
中,这样一旦 session 建立,它就会被调用。
struct ContentView: View {
@EnvironmentObject var viewModel: AuthViewModel
@State var registerUser: Bool = false
@State var selectedIndex = 0
var body: some View {
Group {
if let user = viewModel.currentUser {
MainTabView(user: user, selectIndex: $selectedIndex)
} else if viewModel.userSession == nil {
AppPreview()
} else {
// display something while registering on leave empty
}
}
}
}
class AuthViewModel: ObservableObject {
@Published
var userSession: Session? {
didSet {
if userSession != nil {
register()
}
}
}
@Published
var currentUser: User?
func register() {
if userSession != nil && currentUser == nil {
regiterUserAppleBlog(uid: ACFF.UID ?? "") { (isExist) -> (Void) in
if !isExist {
print("User exists")
} else {
print("User missing")
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.