xcode tableviewcell 的解析/Swift 问题“二元运算符‘==’不能应用于单元格和 nil 类型的操作数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28992785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse/Swift Issue with tableviewcell "binary operator '==' cannot be applied to operands of type cell and nil"
提问by James Moriarty
I've an issue with Parse/Swift using Xcode 6.3 beta
我在使用 Xcode 6.3 beta 时遇到 Parse/Swift 问题
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath , object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! secTableViewCell
if cell == nil
{
cell = secTableViewCell(style: UITableViewCellStyle.Default , reuseIdentifier: "cell")
}
// Configure the cell...
cell.title.text = (object["exams"] as! String)
cell.img.image = UIImage(named: "109.png")
return cell
}
The Error pointed to
错误指向
if cell == nil
{
cell = secTableViewCell(style: UITableViewCellStyle.Default , reuseIdentifier: "cell")
}
binary operator '==' cannot be applied to operands of type cell and nil"
二元运算符“==”不能应用于单元格和 nil 类型的操作数”
回答by Jeffery Thomas
cell
is of type secTableViewCell
not secTableViewCell?
(Optional<secTableViewCell>
). Because it's not an optional, it cannot be nil.
cell
secTableViewCell
不是secTableViewCell?
( Optional<secTableViewCell>
)类型。因为它不是可选的,所以它不能为零。
If you need to test for nil
, then you want to have
如果您需要测试nil
,那么您希望拥有
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? secTableViewCell
The thing is you should never have to test for nil
. "cell" should always be the same type (in your case it should alway be secTableViewCell
.
问题是您永远不必测试nil
. “单元格”应该总是相同的类型(在你的情况下它应该总是secTableViewCell
.