简体   繁体   English

类型 '()' 不能符合 'View'; 只有 struct / enum / class 类型可以符合协议

[英]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 类型可以符合协议”

Image图像

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.

相关问题 navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 请帮忙:类型'()'不能符合'View'; 只有结构/枚举/类类型可以符合协议 - Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 '() -> ()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM