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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:49:02  来源:igfitidea点击:

Parse/Swift Issue with tableviewcell "binary operator '==' cannot be applied to operands of type cell and nil"

iosxcodeswiftparse-platformtableviewcell

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

cellis of type secTableViewCellnot secTableViewCell?(Optional<secTableViewCell>). Because it's not an optional, it cannot be nil.

cellsecTableViewCell不是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.