Swift4+Firebase开发报错求助:AuthDataResult无uid等问题
Let's work through your problems step by step, along with fixing hidden syntax and logic issues in your code:
1. "Value of type 'AuthDataResult' has no member 'uid'" Error
This is a breaking change from Firebase Auth SDK updates. Older versions returned a User? directly in the signIn callback, but now it wraps the user in an AuthDataResult? object. To access the user's uid, you need to pull the user property from this wrapper:
- Swap
self.userUid = user.uidforself.userUid = user.user.uid - Note: Your initial fix to remove the
?fromAuth.auth()?.signInwas correct—Auth.auth()returns a non-optionalAuthinstance, so the question mark was unnecessary.
2. "Only instance methods can be declared @IBAction" Error
Looking at your code, the @IBAction func signInTapped is nested inside the prepare(for:sender:) method! You forgot to close the prepare method with a } before declaring the action. This turns signInTapped into a nested function instead of an instance method of your ViewController class, which triggers the error.
Additional Fixes for Hidden Errors
There are a few more issues that would cause extra errors:
- Swift class names follow UpperCamelCase, so
userVCshould beUserVC - You can't assign a
Stringdirectly to aUITextFieldproperty—you need to set the text field'stextproperty instead - There's an extra closing
}at the end of your code that breaks syntax
Fixed Full Code
import UIKit import Firebase class ViewController: UIViewController { @IBOutlet weak var emailField: UITextField! @IBOutlet weak var passwordField: UITextField! var userUid: String! func goToCreateUserVC(){ performSegue(withIdentifier: "SignUp", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "SignUp" { if let destination = segue.destination as? UserVC { if userUid != nil { destination.userUid = userUid } if let emailText = emailField.text { destination.emailField.text = emailText } if let passwordText = passwordField.text { destination.passwordField.text = passwordText } } } } // Added closing bracket for prepare method @IBAction func signInTapped(_ sender: Any) { if let email = emailField.text, let password = passwordField.text { Auth.auth().signIn(withEmail: email, password: password, completion: { (authResult, error) in if error == nil { if let result = authResult { self.userUid = result.user.uid self.goToCreateUserVC() } } else { self.goToCreateUserVC() } }) } } } // Removed extra closing bracket here
Key Changes Breakdown:
- Closed the
preparemethod properly sosignInTappedbecomes a valid instance method - Renamed
userVCtoUserVCto follow Swift naming conventions - Updated the Firebase Auth callback to use
authResult(a more descriptive name) and accessresult.user.uid - Fixed text field assignment in
prepareto set thetextproperty instead of assigning a string directly to the text field instance - Removed extra syntax (semicolon, closing bracket) that caused errors
内容的提问来源于stack exchange,提问作者Mar Dillo

