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

后端 未结 4 1143
迷失自我
迷失自我 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:52

    You can use the optional chaining syntax for this call (setting cell.textLabel.text to nil if indexPath is nil):

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

    or explicitly unwrap it (causing a runtime error if indexPath is nil):

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

    or use the more verbose if let syntax suggested by @NateCook.

提交回复
热议问题