Matrosov Alexander Matrosov Alexander - 4 months ago 11
Swift Question

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

I am passing trough the block NSError and get this error:

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


This is code I requested from my component that should return error if smth:

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


This is my component interface:

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


This is how my code looks like:

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

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

} else {

}
}

Answer

That is because your error parameter in completion block in not Optional type. and in swift if you want to check for nil values mark you parameter or variables with Optional.

for above try this.

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

By adding ? or ! to the property or parameter it will become Optional. and you can check for nill value.

Comments