无法修复此错误:缺乏更多上下文,表达式类型不明确。

huangapple go评论68阅读模式
英文:

I can't fix this error: Type of expression is ambiguous without more context

问题

以下是您提供的代码的中文翻译部分:

我正在尝试为我的应用程序实现Google登录选项,但我一直遇到错误。

我在网上搜索了一下这个错误的含义,但有人说它是由于参数名称错误引起的。

但我不认为参数名称有问题。

这是我的代码:

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 {
                        
                        // 上传图片
                        
                        if ((user?.profile?.hasImage) != nil) {
                            guard let url = user?.profile?.imageURL(withDimension: 200) else {
                                return
                            }
                            
                            URLSession.shared.dataTask(with: url) { data, _, _ in
                                guard data != nil 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("使用Google登录失败:\(error)")
                }
                return
            }
            print("成功登录用户")
            NotificationCenter.default.post(name: .didLogInNotification, object: nil)
            strongSelf.navigationController?.dismiss(animated: true, completion: nil)
        }
    }
}

您提供的代码翻译完成。如果您有任何其他问题或需要进一步帮助,请随时提出。

英文:

I am trying to implement a google signin option to my application but I keep getting an error

I searched online, on what this error means, but people were saying it is causes by wrong argument names.

but I don't see anything wrong with this.

this is my code:

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)
}
}
}

The same error occurs several times in my code.
Is it possible I am missing some packages?

答案1

得分: 1

要调试"模糊"错误,请简化代码,直到错误消失,并查看是什么导致了它。例如,删除每个闭包的整个内容,看看错误是否消失。然后逐步添加片段。删除已知无误的部分,以简化其余表达式,并帮助编译器提供更好的错误信息。将各个片段提取到函数中,这通常会提供更好的错误消息(而且是更好的代码)。即使没有错误,多个嵌套闭包的单个 85 行方法也应该拆分开来。

英文:

To debug "ambiguous" errors, simplify the code until the error goes away, and see what's creating it. For example, remove the entire contents of each closure and see if the error goes away. Then add pieces back, bit by bit. Delete parts that are known to be ok to simplify the rest of the expressions and help the compiler give you a better error. Extract individual pieces into functions, which will often give better error messages (and be better code anyway). A single 85-line method with multiple nested closures should be split up even if you weren't getting errors.

huangapple
  • 本文由 发表于 2023年2月27日 00:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定