【问题标题】:Swift 4 How can I get the first 30 of a string [duplicate]Swift 4 如何获取字符串的前 30 个 [重复]
【发布时间】:2018-02-25 21:11:31
【问题描述】:

我刚刚更新到 Swift 4,我发现很难显示字符串的前 30 个字符。我有一个通过 Json 获取数据并填充标签的 TableView。现在由于不能再使用子字符串,我很难让它工作。我实际上是在检查返回的字符串是否有 30 个或更多字符,如果有,那么我只想显示这 30 个字符,否则会显示所有内容,因为它不到 30 个字符......这是我的代码

 if streamsModel.Posts[indexPath.row].count >= 30
       {
        // Show 30 characters here
        cell.post.text = streamsModel.Posts[indexPath.row]   
       //.substringFromIndex(cell.post.text.endIndex.advancedBy(-4))

        }
        else
       {
        cell.post.text = streamsModel.Posts[indexPath.row]
        }
        cell.post.tag = indexPath.row

【问题讨论】:

  • 副本也适用于 firstString(streamsModel.Posts[indexPath.row].prefix(30))
  • 如果这不再是重复的,那么您需要解释为什么它不同

标签: swift substring swift4


【解决方案1】:

使用prefix:

cell.post.text = String(streamsModel.Posts[indexPath.row].prefix(30))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 2018-05-25
    • 2011-04-16
    • 2012-06-21
    • 1970-01-01
    • 2014-02-04
    • 2017-08-29
    • 2014-02-15
    相关资源
    最近更新 更多