0

I have made a String extension for validating the form in Swift 3 language. The code is below:

import UIKit
extension String {
    // Validating Email ID
    func isValidEmail(testStr:String) -> Bool {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        return emailTest.evaluate(with: testStr)
    }
    // Validating the User name
    func isValidUserName(testStr:String) -> Bool {
        let RegEx = "\\A\\w{7,18}\\z"
        let Test = NSPredicate(format:"SELF MATCHES %@", RegEx)
        return Test.evaluate(with: testStr)
    }
    // Validating the phone number
    var isPhoneNumber: Bool {
        do {
            let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
            let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.characters.count))
            if let res = matches.first {
                return res.resultType == .phoneNumber && res.range.location == 0 && res.range.length == self.characters.count
            } else {
                return false
            }
        } catch {
            return false
        }
    }
    // validating the password 
    /*
    Use the function of Swift 3.0.
         1. 8 characters length
         2. alphabet
         3. special character
         regex Syntax Explanation :
         (?=.[a-z]) for Character.
         (?=.[$@$#!%?&]) for special character.
         {8,} for length which you want to prefer.
     */
    func isPasswordValid(_ password : String) -> Bool{
        let passwordTest = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[a-z])(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}")
        return passwordTest.evaluate(with: password)
    }
    // validating the password and confirm password are same........................
    func isPasswordSame(password: String , confirmPassword : String) -> Bool {
        if password == confirmPassword{
            return true
        } else {
            return false
        }
    }
    // validating Blank Text........................
    var  isBlank:Bool {
        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
    }
}

But when i am trying to use this extension class in other view controller through the code :

   if isValidEmail("kirit@gmail.com"){
            print("Validate EmailID")
        }
        else{
            print("invalide EmailID")
        }

I am getting the error: Error Shown

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Jessica thompson
  • 407
  • 2
  • 8
  • 20
  • 3
    Is think you want class func method, to call `String.isValidEmail("myString")`, or rather still instances ones `"myString".isValidEmail()`, but in both cases you may want to revise the declaration of the methods. Often, if you don't use `self` in an instance method, or one of its property, you may have thing wrong (forgot something, wrong design, etc.) – Larme May 13 '17 at 21:13
  • 1
    You need to fix your extension because you don't need to pass any parameters when extending your type. – Leo Dabus May 13 '17 at 21:15
  • check this http://stackoverflow.com/questions/31954416/regex-for-alphanumeric-without-special-characters-swift-ios/31954533#31954533 it might help you understand – Leo Dabus May 13 '17 at 21:24

2 Answers2

3

isValidEmail is not a loose function. You have defined it as an instance function on String. You would need to say

"someString".isValidEmail(testStr:"someOtherString")

That makes no sense, but that's how you've configured it. If you write it that way, your code will compile (though it will be very silly code).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • how to get it fixed my extension? – Jessica thompson May 13 '17 at 21:19
  • @JessicaRoot. You need to understand the difference between Instance and Class, and related topics Instance Methods vs Class Methods. It's a basic Oriented Object Programming topic. There should be question here on SO about that. Once you do, apply it in Swift. – Larme May 13 '17 at 21:22
  • I would have defined it as `func isValidEmail() -> Bool`, using `self` where you use `testStr`. – matt May 14 '17 at 00:24
0

Just change your definition to something like

extension String {
// Validating Email ID
func isValidEmail() -> Bool {
    self.validate...
}

and then use it in your code as

@someString".isValidEmail()
Russell
  • 5,436
  • 2
  • 19
  • 27