Learn Courses My Dashboard

Error messages for IBAction Signuptapped & error label

I am following the Firebase Authentication tutorial video and keep on getting these errors. Is there something that I am missing or am not noticing?


Can you please post it as text, not a screenshot. You can do this by placing three backticks on the line before your code and three backticks on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Also add a comment on the line and what the error is

1 Like

This is the line of code where I get an error message of “Only instance methods can be declared @IBAction

@IBAction func signUpTapped( _ sender: Any ) {

For the bottom two line of codes I get and error message of “Value of type ‘SignUpViewController’ has no member ‘showError’” and an error message of "Value of type ‘SignUpViewController’ has no member ‘transitionToHome’

self.transitionToHome()

self.showError("Error creating user")

Hope it’s better now. I had posted the screenshot just in case there was something I missed in the previous lines.

Can you paste this entire file?

It helps to see the “whole picture” all of your code, cause it might be something outside of those lines causing the problem sometimes

1 Like

Ok will do, thanks


    @IBOutlet weak var nameTextField: UITextField!
    
    @IBOutlet weak var lastNameTextField: UITextField!
    
    @IBOutlet weak var emailTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    
    @IBOutlet weak var signUpButton: UIButton!
    
    @IBOutlet weak var errorLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setUpelements()
    }
    
    func setUpelements() {
        
        // Hide the error label
        errorLabel.alpha = 0
        
        //Style the elements
        Utilities.styleTextField(nameTextField)
        Utilities.styleTextField(lastNameTextField)
        Utilities.styleTextField(emailTextField)
        Utilities.styleTextField(passwordTextField)
        Utilities.styleFilledButton(signUpButton)
    }
    
    // Check the fields and validate that the data is correct. If everything is correct, this method returns nil. otherwise, it returns the error message
    func validateFields() -> String? {
        
       // Check that all fields are filled in
        if nameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            lastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
            
            return "Please fill in all fields."
        
        // Check if password is secure.
            let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            
           if Utilities.isPasswordValid(cleanedPassword) == false {
            // Password isn't secure enough
                return "Please make sure your password is at least 8 characters, contains a special character and a number."
            }
            
        return nil
    }

        @IBAction func signUpTapped(_ sender: Any) {
        
        // Validate the fields
        let error = validateFields()
        
        if error != nil {
            
            // There's something wrong with the fields, show error message
            showError(error!)
        }
        
        else {
            // Create cleaned versions of the data
            let name = nameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            let lastName = lastNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            
            // Create the user
            Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
                
                // Check for errors
                if error != nil {
                    //There was an error creating the user suppose to be usinf self. before show but not working
                    self.showError("Error creating user")
                   
                }
                else {
                    
                    // User was created successfully, now store the first name and last name
                    let db = Firestore.firestore()
                    
                    db.collection("users").addDocument(data: ["name":name,"lastname":lastName, "uid":result!.user.uid]) { (error) in
                        if error != nil {
                            // Show error message
                            self.showError("Error saving user data")
                        }
                }
                // Transition to the home screen
                    self.transitionToHome()
                    
                }
            
        }
    
    }
    
    func showError(_ message:String) {
            
        errorLabel.text = message
        errorLabel.alpha = 1
    }
    
        func transitionToHome() {
            
            let homeViewController =
                storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
            
            view.window?.rootViewController = homeViewController
            view.window?.makeKeyAndVisible()
        }
}
}
}

You’re missing a curly braces somewhere
Just looking at validateFields() You’re missing a closing } for the first if statement

Next time paste your entire View Controller.

Highlight all your code and click ctrl + i to properly indent your code. If it doesn’t indent right, you probably are missing a bracket

Got it okay. Sorry still new at all of this, getting use to it. Thank you for your help. It looks like it was } missing that fixed the @IBAction part. Was the other error message because I was not using errorLabel and was using showError instead?

Also for the self.transitionToHome is it because it’s not indented appropriately?

When I remove self. the error wasn’t there anymore

“Has no member” means it can’t find the definition of a function or variable

Fix the curly braces and then build the project again, do you still get any errors

Also when coding a function create the function name add the parenthesis and immediately write the curly braces. This ensure you never forget them

1 Like

Sometimes you need self, sometimes you don’t, it depends on where your writing what code.

Usually though it doesn’t cause any errors if you have it, but don’t need it.

Got it okay. Thank you!

The { bracket did remove the error for the IBAction. The only error I have left is the one for self.transitionToHome
It’s still showing
“Value of type ‘SignUpViewController’ has no member ‘transitionToHome’”

Now I am getting these two errors (which weren’t there before)

        
        // Check that all fields are filled in
        if nameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            lastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
            
            return "Please fill in all fields."
            
            // Check if password is secure.
            let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            
            if Utilities.isPasswordValid(cleanedPassword) == false {
                // Password isn't secure enough
                return "Please make sure your password is at least 8 characters, contains a special character and a number."
            }
            
            return nil
        }
    }