You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift4+Firebase开发报错求助:AuthDataResult无uid等问题

Fixes for Your Xcode 9/Swift 4/Firebase Issues

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.uid for self.userUid = user.user.uid
  • Note: Your initial fix to remove the ? from Auth.auth()?.signIn was correct—Auth.auth() returns a non-optional Auth instance, 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 userVC should be UserVC
  • You can't assign a String directly to a UITextField property—you need to set the text field's text property 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 prepare method properly so signInTapped becomes a valid instance method
  • Renamed userVC to UserVC to follow Swift naming conventions
  • Updated the Firebase Auth callback to use authResult (a more descriptive name) and access result.user.uid
  • Fixed text field assignment in prepare to set the text property instead of assigning a string directly to the text field instance
  • Removed extra syntax (semicolon, closing bracket) that caused errors

内容的提问来源于stack exchange,提问作者Mar Dillo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:31:00