William T.
William T.

Reputation: 14381

NSError code check: Binary operator '==' cannot be applied to two Int operands

Can someone tell me what I'm doing wrong here? "error" is NSError returned from CloudKit.

if error.code == Int(CKErrorCode.NetworkFailure) {
    //do something
}

Gives me this error:

Binary operator '==' cannot be applied to two Int operands

If I do this, it works fine:

if error.code == 4 {
    //do something
}

Where 4 is the actual error code.

Upvotes: 2

Views: 566

Answers (1)

Tom Elliott
Tom Elliott

Reputation: 1926

The problem here is that Int doesn't have a constructor that takes CKErrorCode as input.

As in the comments, the way to compare the two values would be:

if error.code == CKErrorCode.NetworkFailure.rawValue {
    //do something
}

Thankfully, the error messages have been improved for XCode 7 and Swift 2, so you would see:

Cannot invoke initializer for type 'Int' with an argument list of type '(CKErrorCode)'

Which is a much better indicator of what went wrong.

Upvotes: 4

Related Questions