错误:具有getter/setter的变量不能具有初始值 [英] Error: Variable with getter/setter cannot have an initial value

查看:83
本文介绍了错误:具有getter/setter的变量不能具有初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此错误?

与getter/setter一起使用的变量不能具有初始值

Variable with getter/setter cannot have an initial value

这是我的代码:

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

推荐答案

我认为您还有另外一个{}集,它是在定义一个块并将其分配给单元格变量:

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

而且,由于dequeueReusableCell...已经返回了UITableViewCell,所以您真正需要的是:

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
}

这篇关于错误:具有getter/setter的变量不能具有初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆