KyleThe1st KyleThe1st - 14 days ago 7
Swift Question

Compile Time Error

let cell = UITableViewCell()
let dic = dict as Dictionary
let z = "Period \(String(Int(indexPath.row + 1)))" // something wrong with this
let x: Array = dic[z] as! Array
if (x[0] is String) {
cell.textLabel?.text = x[0] as? String
} else {
print("Error: it isnt a string")
}

tableView.reloadData()
return cell


What is wrong with this I feel like it is something simple but I just can't see it if you can see can you please help me! Thank you.

When I run this I get a segmentation fault and I have narrowed it down to this code. Again pls help. ;-;

Answer

Change the line to:

let z = "Period \(indexPath.row + 1)"

You don't need all of the casts. IndexPath.row is already an Int and there's no need to create a String from the Int.

You also have a few other serious issues. Assuming this code is in your cellForRowAt method, NEVER call reloadData() there. Just return a cell. Don't do anything else to the table in that method.

You should also properly dequeue a cell from the tableview. It will make your table view much more efficient.