问题
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