Swift一元运算符'!'不能应用于'OLVideoCell'类型的操作数

时间:2016-04-27 07:43:52

标签: swift

  //This is in the UITableViewCell class method
  class func videoCellWithTableView(tableview:UITableView) -> OLVideoCell{

  var cell = tableview .dequeueReusableCellWithIdentifier("OLVideoCell") as! OLVideoCell

  // “!cell” Why you will be prompted “ Unary operator '!' cannot be applied to an operand of type 'OLVideoCell'”

  if !cell {

        cell = OLVideoCell(style: .Default, reuseIdentifier: "OLVideoCell")

        cell.selectionStyle = .None

    }
    return cell
}

2 个答案:

答案 0 :(得分:1)

if cell == nil,if语句中任何条件的值必须具有符合BooleanType协议的类型。条件也可以是可选的绑定声明。

答案 1 :(得分:1)

重写你的代码:

import UIKit

class OLVideoCell: UITableViewCell {

    class func videoCellWithTableView(tableview: UITableView) -> OLVideoCell {
        // Use `as?` to allow `nil` as a result.
        var cell = tableview.dequeueReusableCellWithIdentifier("OLVideoCell") as? OLVideoCell
        // The condition has to be of boolean type.
        if  cell == nil {
            cell = OLVideoCell(style: .Default, reuseIdentifier: "OLVideoCell")
            cell!.selectionStyle = .None
        }
        return cell! // And the result has to be non-optional.
    }

}