Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toll-free bridging of CFError to NSError not working in Swift 3

Converting our codebase to Swift 3, I have this problem:

ABAddressBookRequestAccessWithCompletion(addressBookRef) { (granted: Bool, error: CFError?) in
        DispatchQueue.main.async {

            if let nsError = error as NSError {
                ...   
            }
        }
}

Compiler error is: Cannot convert value of type 'CFError?' to type 'NSError' in coercion


Changing to:

if let nsError = error as? NSError { ... }

Gives the warning: Cast from 'CFError?' to unrelated type 'NSError' always fails

like image 660
OdieO Avatar asked Nov 14 '16 18:11

OdieO


1 Answers

Do not attempt to pass thru NSError at all. Coerce straight to Error, the Swift type.

if let err = error as? Error {
    print(err) // no problem
}
like image 64
matt Avatar answered Nov 15 '22 09:11

matt