Esqarrouth
Esqarrouth

Reputation: 39181

Getting string value of indexPath.row

Whats the difference between these 2 ways to get string value of indexPath.row?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var newString: String = String(indexPath.row)
    var newString2: String = indexPath.row.description)
}

Upvotes: 1

Views: 6416

Answers (2)

Vijay Rathod
Vijay Rathod

Reputation: 197

var indexValue :String = String(format: "%d", indexPath.row)

convert indexPath into String and Store into indexValue variable.

Upvotes: 1

Nikita Arkhipov
Nikita Arkhipov

Reputation: 1258

The description property is used to represent the content of any NSObject or any class/struct/enum implementing Printable protocol in format of string and though in case of Int (indexPath.row is Int) it returns just string value of number you should never use it to convert Int to String. It is possible that in future versions of Swift description property will return something like "Int: [value]". You should use String(indexPath.row) instead.

Upvotes: 5

Related Questions