簡體   English   中英

Swift:Type沒有確認協議'BooleanType.Protocol'

[英]Swift: Type does not confirm to protocol 'BooleanType.Protocol'

我在嘗試檢查是否設置了可選變量時遇到錯誤。

Error: Type CGPoint? does not confirm to protocol 'BooleanType.Protocol'

這是我的代碼:

var point : CGPoint?

if (point) {
   ...
}

這不是應該如何使用Swift中的可選類型嗎?

應如何編寫if-comparison?

從beta 5開始,你應該寫point == nilpoint != nil

當值是可選的布爾值時,由於混淆而進行了此更改。 例如:

let maybe : Bool? = false
if maybe {
    // executed because `maybe` is an optional having a value (false),
    // not because it is true
}

您也可以像以前一樣使用條件賦值:

if let assignedPoint = point { 
    /* assignedPoint is now a CGPoint unwrapped from the optional */ 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM