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

蓝咒 提交于 2019-12-11 00:35:34

问题


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
  }
}

回答1:


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
}


来源:https://stackoverflow.com/questions/36002551/error-variable-with-getter-setter-cannot-have-an-initial-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!