简体   繁体   English

正则表达式不再起作用(Swift 1.2)

[英]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" 但是自从迅速进行1.2更新以来,我收到一条错误消息:“条件绑定中的绑定值必须是Optional类型”

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: 在Swift 1.2中不再是一个失败的初始化程序,因此您不能(也不必)使用可选绑定测试返回值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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