Error in code (login page)


So I’ve been following the custom login page tutorial and I am almost done. However, I have an error and I do not know why. it’s in reference to “return nil” and says: “‘nil’ is incompatible with return type ‘String’”. Please check the code below and let me know how to rectify the issue.
Many thanks,

    // Check if the 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, contaisn a special character and a number."
    return nil

func signUpTapped(_ sender: Any) {

//validate the fields
let error = validateFields()

if error != nil {
    // There's soemthing wrong with the fields, show error message


Hi Yasmin,

Welcome to the code crew community.

I would say that your function declaration is:

func validateFields() -> String {

rather than:

func validateFields() -> String? {

In other words the ? indicates that return is Optional meaning that the function could return nil.

So, if you add the ? after the word String you should be good to go.

Chris Parker