【发布时间】: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
【问题讨论】:
-
副本也适用于 first:
String(streamsModel.Posts[indexPath.row].prefix(30)) -
如果这不再是重复的,那么您需要解释为什么它不同