Sonny Sluiter
Sonny Sluiter

Reputation: 139

Error: Variable with getter/setter cannot have an initial value

How do I fix this error?

Variable with getter/setter cannot have an initial value

Here's my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell {     //Error
    cell.textLabel?.text = self.items[indexPath.row] //Error
    return cell;  //Error
  }
}

Upvotes: 2

Views: 2305

Answers (1)

David Berry
David Berry

Reputation: 41226

I think you have an extra set of {} As it is, you're defining a block and assigning it to the cell variable:

var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row] //Error
return cell;  //Error

And, since dequeueReusableCell... returns a UITableViewCell already, all you really need is:

var cell = tableview.dequeueReusableCellWithIdentifier("cell")

My guess is you cut/copied code that started out looking like:

if let cell = tableview.dequeueReusableCellWithIdentifier("cell") as? MyCellClass {
    // some setup code here
    return cell
}

Upvotes: 4

Related Questions