调用函数后代码异常,报错‘Use of local variable 'displayAlert' before its declaration’
Hey there! That error is a super common gotcha in Swift—let's sort this out quickly.
What's causing the error?
Swift reads your code top-to-bottom, so if you try to call a function or use a variable before you've defined it, the compiler has no idea what you're referring to. In your case, you're invoking displayAlert somewhere in your ViewController code, but the compiler hasn't seen its declaration yet.
How to fix it
Here are two straightforward solutions based on typical use cases:
1. Move displayAlert's declaration above where you call it
If displayAlert is a helper function in your ViewController, make sure its definition comes before any function that calls it. For example:
import UIKit import FirebaseAuth class ViewController: UIViewController { @IBOutlet weak var dropoffLabel: UILabel! @IBOutlet weak var pickupLabel: UILabel! @IBOutlet weak var passwordTextField: UITextField! @IBOutlet weak var emailTextField: UITextField! // Added since your snippet cut off // First, define your displayAlert function func displayAlert(title: String, message: String) { let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default)) present(alertController, animated: true) } // Then your modified function (previously IBAction) that uses displayAlert func handleAuthAction() { guard let email = emailTextField.text, let password = passwordTextField.text, !email.isEmpty, !password.isEmpty else { // Now displayAlert is already declared, so this call works displayAlert(title: "Missing Info", message: "Please fill in both email and password fields") return } // Firebase Auth logic here Auth.auth().signIn(withEmail: email, password: password) { [weak self] result, error in guard let self = self else { return } if let error = error { self.displayAlert(title: "Authentication Failed", message: error.localizedDescription) } else { self.displayAlert(title: "Success", message: "You're logged in!") // Navigate to next screen or update UI here } } } // Rest of your ViewController code (viewDidLoad, etc.) }
2. Check for local variable misordering
If displayAlert is a local variable/function inside another function (not a ViewController method), you need to declare it before you use it. For example, don't do this:
func badFunction() { displayAlert(title: "Oops", message: "This will fail") // ❌ Called before declaration func displayAlert(title: String, message: String) { // Alert code here } }
Instead, reorder it:
func goodFunction() { func displayAlert(title: String, message: String) { // Alert code here } displayAlert(title: "Works!", message: "No more errors") // ✅ Called after declaration }
Quick extra tip
Since you converted an @IBAction to a regular function, don't forget to trigger it somewhere (like connecting it to a button's touch event programmatically in viewDidLoad, or calling it from another part of your logic). That's unrelated to the current error, but it's easy to overlook after refactoring!
内容的提问来源于stack exchange,提问作者Tawakal ali

