Value of type 'UITextField?' has no member 'isValidEmail'

import UIKit
import Firebase



class SignUpViewController: UIViewController {
    
    //Outlets
    @IBOutlet weak var firstNameText: UITextField!
    @IBOutlet weak var lastNameText: UITextField!
    @IBOutlet weak var emailText: UITextField!
    @IBOutlet weak var passwordText: UITextField!
    @IBOutlet weak var signUpButton: UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    
    @IBAction func signUpButtonTapped(_ sender: Any) {
        guard let firstName = firstNameText.text,
            let lastName = lastNameText.text,
            let email = emailText.text else { return }
            guard emailText.isValidEmail() else {
            print("Invalid email/password. Please try again.")
            return
        }
            guard let password = passwordText.text else { return }
            guard passwordText.isValidPassword() else {
            print("Invalid email/password. Please try again.")
            return
        }

        Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
            if let error = error {
                debugPrint("Error creating user: \(error.localizedDescription)")
            }
            let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
            changeRequest?.displayName = firstName
            changeRequest?.commitChanges(completion: { (error) in
                if let error = error {
                    debugPrint(error.localizedDescription)
                }
            })
            guard let userId = authResult?.user else { return }
            let userData: [String: Any] = [
                "firstName" : "",
                "lastName" : "",
                "User ID" : userId,
                "dateCreated" : FieldValue.serverTimestamp(),
                ]
            let db = Firestore.firestore()
            db.collection("users").document("one").setData(userData) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }
            }
        }
    }
}
protocol Validator {
    associatedtype V
    
    func validate(_ functions: [V]) -> Bool
}

protocol Evaluator {
    associatedtype E
    
    func evaluate(with condition: E) -> Bool
}

extension UITextField: Validator {
    func validate(_ functions: [(String) -> Bool]) -> Bool {
        return functions.map { f in f(self.text ?? "") }.reduce(true) { $0 && $1 }
    }
}

extension String: Evaluator {
    func evaluate(with condition: String) -> Bool {
        guard let range = range(of: condition, options: .regularExpression, range: nil, locale: nil) else {
            return false
        }
        return range.lowerBound == startIndex && range.upperBound == endIndex
    }
}
func isValidEmail(text: String) -> Bool {
    let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    return text.evaluate(with: emailRegex)
}

func isValidPassword(text: String) -> Bool {
    let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$"
    return text.evaluate(with: passwordRegex)
}


Errors:

line 25: Value of type 'UITextField?' has no member 'isValidEmail'

line 30: Value of type 'UITextField?' has no member 'isValidPassword'


I'm trying to implement a email and password validation flow to use for signing up users. I used two different protocols to first evaluate the string from beginning to end and checking UITextField with validator functions using a map.


Could someone please help me understand these errors??

The only method defined for `UITextField` is `validate(_:)`. You cannot expect any other methods, such as `isValidEmail(text:)` or `isValidPassword(text:)`, would work on `UITextField` or `UITextField?` .


Is that really you who dsigned the protocol `Validator` ? As far as I read from your code, you do not understand how to use the protocol properly.


You can use `validate(_:)` on `UITextField` like this:

        guard let firstName = firstNameText.text,
            let lastName = lastNameText.text,
            let email = emailText.text else { return }
        guard emailText.validate([isValidEmail]) else {
            print("Invalid email/password. Please try again.")
            return
        }
        guard let password = passwordText.text else { return }
        guard passwordText.validate([isValidPassword]) else {
            print("Invalid email/password. Please try again.")
            return
        }


Or you can use the two global functions: (The protocol `Validator` has no meaning in this case)

        guard isValidEmail(text: email) else {
            print("Invalid email/password. Please try again.")
            return
        }
        guard let password = passwordText.text else { return }
        guard isValidPassword(text: password) else {
            print("Invalid email/password. Please try again.")
            return
        }


But you have no extensions to make use of `emailText.isValidEmail()` or `passwordText.isValidPassword()` .


--

One more. Your issue has nothing to do with iOS Simulator And Devices. Your code is just wrong as a Swift code.

Value of type 'UITextField?' has no member 'isValidEmail'
 
 
Q