“NSError”类型的值永远不能为零,不允许进行比较

时间:2016-07-29 09:18:11

标签: swift swift2 nserror

我正在通过块NSError并得到此错误:

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

这是我从我的组件请求的代码,如果smth:

应返回错误
- (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 个答案:

答案 0 :(得分:4)

这是因为完成块中的错误参数不是可选类型。并且在swift中如果要检查nil值,请使用Optional标记参数或变量。

上面试试这个。

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

加入?要么 !对于属性或参数,它将成为可选项。你可以检查一下nill值。