SecItemCopyMatching的swift 2.0 keychain类型错误

前端之家收集整理的这篇文章主要介绍了SecItemCopyMatching的swift 2.0 keychain类型错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们使用以前版本的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<>工作了.

尝试:

var retrievedData: NSData?
var extractedData: AnyObject?
let status = SecItemCopyMatching(keyChainQuery,&extractedData)

if (status == errSecSuccess) {
    retrievedData = extractedData as? NSData
}
原文链接:https://www.f2er.com/swift/320037.html

猜你在找的Swift相关文章