【问题标题】:Swift Unary operator '!' cannot be applied to an operand of type 'OLVideoCell'Swift 一元运算符 '!'不能应用于“OLVideoCell”类型的操作数
【发布时间】:2016-04-27 07:43:51
【问题描述】:
  //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
}

【问题讨论】:

  • 因为这是 Swift 而cell 不是Bool。像这样写下你的条件:cell == nil.
  • 谢谢 但是会提示“'OLVideoCell'类型的值永远不能为零,不允许比较”
  • 因为您强制将单元格转换为非可选类型。如果您期望nil 出现,请使用可选类型。

标签: swift


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    像这样重写你的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多