swift 我无法修复此错误:表达式类型不明确,没有更多上下文

mrwjdhj3  于 2023-02-28  发布在  Swift
关注(0)|答案(1)|浏览(162)

我试图实现一个谷歌登录选项到我的应用程序,但我不断得到一个错误
我在网上搜索了这个错误的意思,但是人们说这是由错误的参数名称引起的。
但我看不出这有什么不对。
这是我的代码:

private func googleLogIn() {
        guard let clientID = FirebaseApp.app()?.options.clientID else { return }
        
        let config = GIDConfiguration(clientID: clientID)
        
        GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in
            
            if let error = error {
                print("\(error.localizedDescription)")
                return
            }
            
            guard let email = user?.profile?.email,
                  let firstName = user?.profile?.givenName,
                  let lastName = user?.profile?.familyName else {
                      return
                  }
            
            UserDefaults.standard.set(email, forKey: Keys.email.rawValue)
            UserDefaults.standard.set("\(firstName) \(lastName)", forKey: Keys.name.rawValue)
            
            DatabaseManager.shared.userExists(with: email) { exists in
                if !exists {
                    
                    let chatUser = ChatAppUser(firstName: firstName,
                                               lastName: lastName,
                                               emailAddress: email)
                    DatabaseManager.shared.insertUser(with: chatUser) { success in
                        if success {
                            
                            // upload image
                            
                            if ((user?.profile?.hasImage) != nil) {
                                guard let url = user?.profile?.imageURL(withDimension: 200) else {
                                    return
                                }
                                
                                URLSession.shared.dataTask(with: url) { data, _, _ in
                                    guard let data = data else {
                                        return
                                    }
                                    
                                    let filename = chatUser.profilePictureFileName
                                    StorageManager.shared.uploadProfilePicture(with: data,
                                                                               filename: filename) { result in
                                        switch result {
                                        case .success(let downloadUrl):
                                            UserDefaults.standard.set(downloadUrl, forKey: Keys.profilePictureUrl.rawValue)
                                            print(downloadUrl)
                                        case .failure(let error):
                                            print(error)
                                        }
                                    }
                                }.resume()
                            }
                        }
                    }
                }
            }
            
            guard let authentication = user?.authentication,
                  let idToken = authentication.idToken else {
                      return
                  }
            
            let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                           accessToken: authentication.accessToken)
            
            FirebaseAuth.Auth.auth().signIn(with: credential) { [weak self] authResult, error in
                guard let strongSelf = self else {
                    return
                }
                
                guard authResult != nil, error == nil else {
                    if let error = error {
                        print("Failed to sign in with Google: \(error)")
                    }
                    return
                }
                print("Successfully logged user in")
                NotificationCenter.default.post(name: .didLogInNotification, object: nil)
                strongSelf.navigationController?.dismiss(animated: true, completion: nil)
            }
        }
    }
}

同样的错误在我的代码中发生了好几次。是否可能我丢失了一些包?

bybem2ql

bybem2ql1#

要调试“不明确的”错误,简化代码直到错误消失,并查看是什么造成了错误。例如,删除每个闭包的整个内容,看看错误是否消失。然后一点一点地添加回部分。删除已知可以的部分,以简化表达式的其余部分,并帮助编译器给予更好的错误。将单个部分提取到函数中,这通常会给予更好的错误信息(并且无论如何都是更好的代码)。一个包含多个嵌套闭包的85行方法应该被拆分,即使你没有得到错误。

相关问题