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

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

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

问题

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

  1. 我正在尝试为我的应用程序实现Google登录选项,但我一直遇到错误。
  2. 我在网上搜索了一下这个错误的含义,但有人说它是由于参数名称错误引起的。
  3. 但我不认为参数名称有问题。
  4. 这是我的代码:
  5. private func googleLogIn() {
  6. guard let clientID = FirebaseApp.app()?.options.clientID else { return }
  7. let config = GIDConfiguration(clientID: clientID)
  8. GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in
  9. if let error = error {
  10. print("\(error.localizedDescription)")
  11. return
  12. }
  13. guard let email = user?.profile?.email,
  14. let firstName = user?.profile?.givenName,
  15. let lastName = user?.profile?.familyName else {
  16. return
  17. }
  18. UserDefaults.standard.set(email, forKey: Keys.email.rawValue)
  19. UserDefaults.standard.set("\(firstName) \(lastName)", forKey: Keys.name.rawValue)
  20. DatabaseManager.shared.userExists(with: email) { exists in
  21. if !exists {
  22. let chatUser = ChatAppUser(firstName: firstName,
  23. lastName: lastName,
  24. emailAddress: email)
  25. DatabaseManager.shared.insertUser(with: chatUser) { success in
  26. if success {
  27. // 上传图片
  28. if ((user?.profile?.hasImage) != nil) {
  29. guard let url = user?.profile?.imageURL(withDimension: 200) else {
  30. return
  31. }
  32. URLSession.shared.dataTask(with: url) { data, _, _ in
  33. guard data != nil else {
  34. return
  35. }
  36. let filename = chatUser.profilePictureFileName
  37. StorageManager.shared.uploadProfilePicture(with: data,
  38. filename: filename) { result in
  39. switch result {
  40. case .success(let downloadUrl):
  41. UserDefaults.standard.set(downloadUrl, forKey: Keys.profilePictureUrl.rawValue)
  42. print(downloadUrl)
  43. case .failure(let error):
  44. print(error)
  45. }
  46. }
  47. }.resume()
  48. }
  49. }
  50. }
  51. }
  52. }
  53. guard let authentication = user?.authentication,
  54. let idToken = authentication.idToken else {
  55. return
  56. }
  57. let credential = GoogleAuthProvider.credential(withIDToken: idToken,
  58. accessToken: authentication.accessToken)
  59. FirebaseAuth.Auth.auth().signIn(with: credential) { [weak self] authResult, error in
  60. guard let strongSelf = self else {
  61. return
  62. }
  63. guard authResult != nil, error == nil else {
  64. if let error = error {
  65. print("使用Google登录失败:\(error)")
  66. }
  67. return
  68. }
  69. print("成功登录用户")
  70. NotificationCenter.default.post(name: .didLogInNotification, object: nil)
  71. strongSelf.navigationController?.dismiss(animated: true, completion: nil)
  72. }
  73. }
  74. }

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

英文:

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:

  1. private func googleLogIn() {
  2. guard let clientID = FirebaseApp.app()?.options.clientID else { return }
  3. let config = GIDConfiguration(clientID: clientID)
  4. GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in
  5. if let error = error {
  6. print("\(error.localizedDescription)")
  7. return
  8. }
  9. guard let email = user?.profile?.email,
  10. let firstName = user?.profile?.givenName,
  11. let lastName = user?.profile?.familyName else {
  12. return
  13. }
  14. UserDefaults.standard.set(email, forKey: Keys.email.rawValue)
  15. UserDefaults.standard.set("\(firstName) \(lastName)", forKey: Keys.name.rawValue)
  16. DatabaseManager.shared.userExists(with: email) { exists in
  17. if !exists {
  18. let chatUser = ChatAppUser(firstName: firstName,
  19. lastName: lastName,
  20. emailAddress: email)
  21. DatabaseManager.shared.insertUser(with: chatUser) { success in
  22. if success {
  23. // upload image
  24. if ((user?.profile?.hasImage) != nil) {
  25. guard let url = user?.profile?.imageURL(withDimension: 200) else {
  26. return
  27. }
  28. URLSession.shared.dataTask(with: url) { data, _, _ in
  29. guard let data = data else {
  30. return
  31. }
  32. let filename = chatUser.profilePictureFileName
  33. StorageManager.shared.uploadProfilePicture(with: data,
  34. filename: filename) { result in
  35. switch result {
  36. case .success(let downloadUrl):
  37. UserDefaults.standard.set(downloadUrl, forKey: Keys.profilePictureUrl.rawValue)
  38. print(downloadUrl)
  39. case .failure(let error):
  40. print(error)
  41. }
  42. }
  43. }.resume()
  44. }
  45. }
  46. }
  47. }
  48. }
  49. guard let authentication = user?.authentication,
  50. let idToken = authentication.idToken else {
  51. return
  52. }
  53. let credential = GoogleAuthProvider.credential(withIDToken: idToken,
  54. accessToken: authentication.accessToken)
  55. FirebaseAuth.Auth.auth().signIn(with: credential) { [weak self] authResult, error in
  56. guard let strongSelf = self else {
  57. return
  58. }
  59. guard authResult != nil, error == nil else {
  60. if let error = error {
  61. print("Failed to sign in with Google: \(error)")
  62. }
  63. return
  64. }
  65. print("Successfully logged user in")
  66. NotificationCenter.default.post(name: .didLogInNotification, object: nil)
  67. strongSelf.navigationController?.dismiss(animated: true, completion: nil)
  68. }
  69. }
  70. }

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:

确定