在Swift 3中不能免费桥接CFError到NSError



将我们的代码库转换为Swift 3,我有这样的问题:

ABAddressBookRequestAccessWithCompletion(addressBookRef) { (granted: Bool, error: CFError?) in
        DispatchQueue.main.async {
            if let nsError = error as NSError {
                ...   
            }
        }
}

编译错误是:Cannot convert value of type 'CFError?' to type 'NSError' in coercion


改变:

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

给出警告:Cast from 'CFError?' to unrelated type 'NSError' always fails

不要尝试通过NSError。直接强制到Error, Swift类型。

if let err = error as? Error {
    print(err) // no problem
}

最新更新