我们使用以前版本的Swift获得了这段代码
var retrievedData: NSData? var extractedData: Unmanaged<AnyObject>? = nil let status = SecItemCopyMatching(keyChainQuery,&extractedData) if (status == errSecSuccess) { if let validExtractedData = extractedData { let opaque = validExtractedData.toOpaque() retrievedData = Unmanaged<NSData>.fromOpaque(opaque).takeUnretainedValue() } }
但是现在这给了我们以下错误:
Cannot convert value of type ‘inout Unmanaged?’ (aka
‘inout Optional>’) to expected argument type
‘UnsafeMutablePointer’ (aka
‘UnsafeMutablePointer>’)
我对如何修复它有点迷茫.
我假设我必须分配一些内存,为它创建一个UnsafeMutablePointer,然后将其提供给SecItemCopyMatching?
我试过这个:
let sizeOfKey = 32 var store = NSMutableData(capacity: sizeOfKey)! let status = SecItemCopyMatching(keyChainQuery,store.bytes)
但这给了我一个:
Cannot convert value of type ‘UnsafePointer’ (aka
‘UnsafePointer<()>’) to expected argument type
‘UnsafeMutablePointer’ (aka
‘UnsafeMutablePointer>’) error
有人有想法吗?
看来,我们不需要Unmanaged<>工作了.
原文链接:https://www.f2er.com/swift/320037.html尝试:
var retrievedData: NSData? var extractedData: AnyObject? let status = SecItemCopyMatching(keyChainQuery,&extractedData) if (status == errSecSuccess) { retrievedData = extractedData as? NSData }