以下代码几乎与Apple 文档完全相同,编译时没有错误:
guard let firstItem = (rawItems! as? Array<Dictionary<String, Any>>)?.first else {
throw AnError()
}
let identityRef = firstItem[kSecImportItemIdentity as String]
as! SecIdentity? // !!!
guard let identity = identityRef else {
throw AnError()
}
标记为的行!!!
包含强制向下转换,而替换as!
为as
很明显会导致编译错误'Any?' is not convertible to 'SecIdentity?'...
确实SecIdentity
是一个类,而Any
可能甚至不是一个类。
我真正无法解释的是以下内容。如果我尝试使代码更安全,请使用此
guard let idenity = firstItem[kSecImportItemIdentity as String] as? SecIdentity
else {
throw AnError()
}
或这个
guard let idenityRef = firstItem[kSecImportItemIdentity as String] as? SecIdentity?
else {
throw AnError()
}
我得到一个编译错误:Conditional downcast to CoreFoundation type 'SecIdentity' will always succeed