字符串长度Swift

Jul*_*ius 0 string string-length uitextfield swift

让我疯狂的一件事就是找到一件简单的事:Swift中的字符串长度.

不,你不能使用string.length

不,你不能使用string.characters.count

不,你不能使用string.count

唯一对我有用的是string.endIndex.然而,这对于简单的textField检查不起作用,例如

func textViewDidChange(textView: UITextView) {
    if textView.text.endIndex > 20 {
        setBorder(descriptionField, finished: true)
    } else {
        setBorder(descriptionField, finished: false)
    }
}
Run Code Online (Sandbox Code Playgroud)

由于endIndex不是Int而且无法转换成.这整件事让我发疯,我不知道该怎么办.任何帮助,将不胜感激!

Chr*_*wik 5

只需使用count()

count(textView.text)
Run Code Online (Sandbox Code Playgroud)

在swift 2.0中你提到的一些东西工作=]