NSIndexPath? does not have a member name 'row' error in Swift

后端 未结 4 1145
迷失自我
迷失自我 2020-12-10 11:12

I\'m creating a UITableViewController with Swift language and in a method

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: N         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-10 11:46

    You can either unwrap the optional indexPath parameter with if let...:

    if let row = indexPath?.row {
        cell.textLabel.text = self.dataStore[row]
    }
    

    or if you're sure indexPath isn't nil, you can force the unwrapping with !:

    cell.textLabel.text = self.dataStore[indexPath!.row]
    

    Just keep in mind that indexPath! on a nil value will be a runtime exception, so it's better practice to unwrap it as in the first example.

提交回复
热议问题