【问题标题】:String length Swift [duplicate]字符串长度 Swift [重复]
【发布时间】:2015-10-21 03:59:54
【问题描述】:

让我发疯的事情是找到一个简单的东西: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)
    }
}

由于 endIndex 不是 Int 并且不能转换为 in。这整件事让我发疯,我不知道该怎么办。任何帮助将不胜感激!

【问题讨论】:

    标签: string swift uitextfield string-length


    【解决方案1】:

    只需使用 count()

    count(textView.text)
    

    在 swift 2.0 中你提到的一些东西有效 =]

    【讨论】:

      【解决方案2】:

      Apple 已多次更改处理此问题的方式,这将使许多现有文档过时。

      如果您使用的是 Swift 2 及更高版本,请使用 -

      textView.text.characters.count

      如果你使用的是 Swift 1.2,那就是 -

      count(textView.text)

      【讨论】:

        【解决方案3】:

        在 Swift 2.0 中,它是 string.characters.count

        【讨论】:

          猜你喜欢
          • 2015-06-16
          • 2012-05-22
          • 1970-01-01
          • 2017-08-19
          • 2012-10-23
          • 1970-01-01
          • 2012-08-07
          • 2012-07-14
          相关资源
          最近更新 更多