Reputation: 37581
How do you assign an error code to an NSError in Swift? The following gives a compilation error saying "cannot assign to 'code' in 'e'":
var e = NSError()
e.code = 6;
Also why does the following code generate an error of 'Int is not convertible to selector'
if e.code == EKAuthorizationStatus.Restricted
{
}
Upvotes: 0
Views: 1022
Reputation: 2563
The error code property is read only and of type Int. You need to initialize as follows:
var e= NSError(domain: <string?>, code: 6, userInfo: <AnyObject>)
Update: As @ahruss pointed out you also need to convert the enum to its raw form, since the type conversion is not implicit, I have updated the example accordingly.
Please find the documentation on NSError class here.
Example playground:
import Cocoa
var e = NSError(domain: nil, code: 6, userInfo: nil);
//if e.code == 6
if e.code == EKAuthorizationStatus.Restricted.toRaw()
{
println("success");
}
Upvotes: 7
Reputation: 2130
The other half of the answer (wbennet's answer covered the other part, that code is readonly) is that EKAuthoriziationStatus is an enum, and you don't get implicit conversions in Swift. You need to do this:
if e.code == EKAuthorizationStatus.Restricted.toRaw()
{
...
}
Upvotes: 1