2016-07-29 59 views
0

我傳遞槽塊NSError和得到這個錯誤:值類型「NSError」的永遠是零,比較是不允許

Value of type 'NSError' can never be nil, comparison isn't allowed 

這是代碼,我從我的組件要求應該返回錯誤如果水木清華:

- (void)findPeripheralForDevice:(Device *)device completion:(void (^)(NSError *error, BOOL needsConfigure))completion; 

這是我的組件接口:

func findDeviceWithSerialNumber(serial: String, completion:(error: NSError, needsConfigure: Bool) -> Void) 

這是我的代碼看起來像:

wirlessService.findDeviceWithSerialNumber(serial) { (error, needsConfigure) in 

     if error != nil { // here the error described above occurred 

     } else { 

     } 
    } 
+1

您的'error'參數未聲明爲可選。添加一個可爲空的註解。 https://developer.apple.com/swift/blog/?id=25 – Moritz

+0

@EricD,你是對的,但例如,如果我調用' - (void)findPeripheralForDevice:(Device *)設備完成:(void( ^)(NSError *錯誤,布爾needsConfigure))完成;'直接沒有錯誤問題是stalage –

回答

4

這是因爲您的錯誤參數在完成塊中不是可選類型。如果你想檢查零值,可以用swift標記你的參數或變量。

以上試試這個。

func findDeviceWithSerialNumber(serial: String, completion:(error: NSError?, needsConfigure: Bool) -> Void) 

通過添加?要麼 !到屬性或參數它將成爲可選。你可以檢查價值。

+0

yea同意它,適用於我。奇怪的是,如果我從客觀c代碼中調用塊,那麼NSError沒有問題。 –

相關問題