2016-04-27 117 views
0
//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 
} 
+0

因爲這是斯威夫特和'cell'不是'Bool'。寫下你的條件:'cell == nil'。 – werediver

+0

謝謝但會提示「類型'OLVideoCell'的值不能爲零,不允許比較」 –

+0

因爲您強制將您的單元格轉換爲非可選類型。如果您期望'nil',請使用可選類型。 – werediver

回答

1

if cell == nil,if語句中任何條件的值都必須具有符合BooleanType協議的類型。該條件也可以是可選的綁定聲明。

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

} 
相關問題