如何计算iOS文本字段中的字符数

时间:2015-10-18 02:05:08

标签: ios swift

我想计算用户想要验证密码时的字符数:

到目前为止我用这段代码出来了,但似乎没有用

@IBOutlet var password: UITextField!

 if (Password!.count < 6)
        {
/// Not acceptable
        }

2 个答案:

答案 0 :(得分:12)

怎么样

if password.text?.characters.count < 6 {
    ...
}

答案 1 :(得分:1)

对于Swift 5,我使用了这种方法,并且效果很好

if password.text.count > 6 {
  //add scenario for this case here
}