0

I have just updated to Swift 4 and I am finding it difficult to show the first 30 characters of a string. I have a TableView that gets data via Json and that is populating a label. Now since Substrings can not be used anymore I am having a hard time getting this to work. I am essentially checking if the string returned has 30 or more characters and if it does then I want to show only those 30 characters else show everything since it's under 30 characters .... This is my code

 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
user1949387
  • 1,245
  • 3
  • 21
  • 38

1 Answers1

1

Use prefix:

cell.post.text = String(streamsModel.Posts[indexPath.row].prefix(30))
rmaddy
  • 314,917
  • 42
  • 532
  • 579