1

I had a switch statement in Xcode 7.3 that (after passing error: NSError as an argument) worked like so:

if let code:CKErrorCode = CKErrorCode(rawValue: error.code) {
    switch code {
    case .NotAuthenticated: etc...
    }
}

After migrating to Swift 3.0 in Xcode 8 beta 6, I started getting errors saying that CKErrorCode is not recognized. I'm still importing cloud, and the migration lowercased my enums and I've tried to change it like so:

switch error.code {
case .notAuthenticated.rawValue: etc...
}

but that also seems to error (the beta seems to be buggy on my system so the errors keep disappearing and reappearing, so it's hard to tell as I code now and it may just be my system, but they persist when I compile). I've been scanning https://swift.org/migration-guide/#known-migration-issues and https://swift.org/migration-guide/ but haven't found anything yet and when I google CKErrorCode the documentation (which I'm assuming is ignoring Swift 3 since it's in beta) seems to say my previous syntax is kosher.

Can anyone point me in the right direction on this? Did enums lose .rawValue? Enums seemed to have changed, but I'm having difficulty finding documentation on what I should be doing. Is there an alternative to CKErrorCode that will recognize these enum cases? Please don't tell me cloud kit error handling changed more dramatically than that :) Thanks in advance.

Mercutio
  • 1,152
  • 1
  • 14
  • 33
  • You just need to cast from Swift 3 Error to NSError – Leo Dabus Sep 01 '16 at 16:42
  • My error is passed in as NSError and when I option click `error` it's described as `let error: NSError`...did you mean I need to cast from NSError to Swift 3 Error (I'm kinda confused how the bridging works to be honest). (error as! NSError).code warns that I'm casting from NSError to NSError – Mercutio Sep 01 '16 at 16:47
  • Also is CKErrorCode gone, or do I need to fix something about my configuration? – Mercutio Sep 01 '16 at 16:48
  • 1
    casting to CKError code worked. thanks – Mercutio Sep 01 '16 at 16:50

1 Answers1

1

Conversion to CKError from NSError as shown below:

let nsError: NSError = NSError() // NSError variable. 

let cError = CKError(_nsError: nsError)

let cErrorCode = cError.code
Kevin
  • 828
  • 11
  • 14