【问题标题】:Swift 2 iOS9 text case checkSwift 2 iOS9 文本大小写检查
【发布时间】:2016-03-22 16:51:35
【问题描述】:

我正在尝试为以下示例找到解决方案。我已经创建了一个用户注册页面,我检查了密码重置问题与实际密码不匹配(工作正常),但我想要做的是将输入的密码和密码重置问题转换为小写并检查没有匹配。我更愿意即时执行此操作,而不是将密码放入 var。

这样做的原因是为了确保密码重置问题与实际密码不同,无论特定字符的大小写。

    if (passWord.text) == (resetQuestion.text) {
        let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message:
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }


    if String.lowercaseString.rangeOfString(passWord.text) == String.lowercaseString.rangeOfString(resetQuestion.text) {
        let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message: 
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }

任何指点或帮助将不胜感激。

【问题讨论】:

    标签: swift2 ios9


    【解决方案1】:

    您可以尝试使用 caseInsensitiveCompare 方法。类似的东西

    if passWord.text.caseInsensitiveCompare(resetQuestion.text) == .OrderedSame {
            let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message:
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }
    

    【讨论】:

    • 感谢卢克,效果很好。我只需要添加!在 passWord.text 和 resetQuestion.text 之后。
    • String 不是 Swift 中的类。 lowercaseString 和 uppercaseString 是实例属性,所以 "Ab".uppercaseString == "AB 是 true 等等。
    • @dacrozz 没关系,很高兴它起作用了:)。我看到你必须解开这些值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多