user1406716
user1406716

Reputation: 9705

bound value in conditional binding must be of optional type

in line if let ip = indexPath?, I am getting the following error: bound value in conditional binding must be of optional type

What do I do to indexPath to fix this issue?

<>

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellId:String = "Cell"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell

    if let ip = indexPath? {
        cell.textLabel?.text = myData[ip.row] as String
    }


    return cell
}

Upvotes: 1

Views: 581

Answers (2)

giorashc
giorashc

Reputation: 13713

indexPath in not an optional type (i.e NSIndexPath?) so there is no need to unwrap it with if let ip = indexPath? (hence the error message)

You can use it as is :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let CellId:String = "Cell"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell

    cell.textLabel?.text = myData[indexPath.row] as String

    return cell
}

Upvotes: 2

Midhun MP
Midhun MP

Reputation: 107231

The indexPath is not an optional value. So it's value will not be nil in any condition.

So you can't write like (can't unwrap non-optional value):

if let ip = indexPath?

So change the code like:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let CellId:String = "Cell"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell
    cell.textLabel?.text = myData[indexPath.row] as String
    return cell
}

Upvotes: 0

Related Questions