我目前正在iTunes U上关注斯坦福的Swift课程。我试图按照第10讲中的示例进行操作,并且在下面的代码行中收到以下错误:The operand of postfix '?' should have an optional type; type is 'UILabel'
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
let tweet = tweets[indexPath.section][indexPath.row]
cell.textLabel?.text = tweet.text // this line throws the error
cell.detailTextLabel?.text = tweet.user.name
return cell
}
但是,如果我删除?
它,则编译正常。这与视频中的示例(使用进行编译?
)相冲突。另外,UITableViewCell的文档显示,它textLabel
是可选类型。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
let tweet = tweets[indexPath.section][indexPath.row]
cell.textLabel.text = tweet.text // for some reason it wants me to treat the textLabel as a UILabel and not as an optional
cell.detailTextLabel?.text = tweet.user.name
return cell
}
有人可以解释导致差异的原因吗?
通过安装最新版本的Xcode(版本6.2)已解决了此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句