简体   繁体   中英

Regex No longer working (Swift 1.2)

I've been using:

    func isValidPassword(testStr2:String) -> Bool {
        println("validate password: \(testStr2)")

        let passwordRegEx = "[A-Z0-9a-z._%+-:/><#]{6,30}"

        if let passwordTest = NSPredicate(format: "SELF MATCHES %@", passwordRegEx) {
            return passwordTest.evaluateWithObject(testStr2)
        }

        return false
    }

to check that string has the appropriate characters. But since the swift 1.2 Update, I get an error saying "Bound value in a conditional binding must be of an Optional type"

I've tried adding question marks. But I can't find how to fix the problem. Anyone know what's gone wrong?

extension NSPredicate {
    convenience init(format predicateFormat: String, _ args: CVarArgType...)
}

is not a failable initializer anymore in Swift 1.2, so you cannot (and need not) test the return value with optional binding:

let passwordTest = NSPredicate(format: "SELF MATCHES %@", passwordRegEx)
return passwordTest.evaluateWithObject(testStr2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM