小编典典

SecItemCopyMatching的swift 2.0钥匙串类型错误

swift

我们的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()
        }
    }

但是,这现在给我们带来以下错误:

无法转换“ inout不受管理?”类型的值 (aka’inout
Optional>’)到预期参数类型’UnsafeMutablePointer’(aka’UnsafeMutablePointer>’)

我对如何解决它有些迷茫。我想我必须分配一些内存,为其创建一个UnsafeMutablePointer,然后将其提供给SecItemCopyMatching?

我尝试了这个:

    let sizeOfKey = 32
    var store = NSMutableData(capacity: sizeOfKey)!
    let status = SecItemCopyMatching(keyChainQuery, store.bytes)

但这给了我一个:

无法将类型’UnsafePointer’(aka’UnsafePointer
<()>’)的值转换为预期的参数类型’UnsafeMutablePointer’(aka’UnsafeMutablePointer>’)错误

有人知道吗?


阅读 375

收藏
2020-07-07

共1个答案

小编典典

看来,我们不再需要Unmanaged<>工作。

尝试:

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

if (status == errSecSuccess) {
    retrievedData = extractedData as? NSData
}
2020-07-07