本文介绍了SecItemCopyMatching的swift 2.0钥匙串类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Swift的早期版本中有了这段代码

We had this snippet of code with the previous version of 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()
        }
    }

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

However this now gives us the following error:

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

I'm a bit lost on how to fix it. I presume I have to allocate some memory, make an UnsafeMutablePointer to it and then supply this to SecItemCopyMatching?

我尝试过:

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

但这给了我一个:

有人知道吗?

推荐答案

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

It seems, we don't need Unmanaged<> work anymore.

尝试:

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

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

这篇关于SecItemCopyMatching的swift 2.0钥匙串类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:54