如何在 Xcode 7 beta 4 中调用 SecItemCopyMatching?

How to call SecItemCopyMatching in Xcode 7 beta 4?

在带有 Swift 的 Xcode 6 和 7 的先前版本中,此语法将起作用:

    var secureItemValue: Unmanaged<AnyObject>?

    let statusCode: OSStatus = SecItemCopyMatching(keychainItemQuery, &secureItemValue)
    if statusCode == errSecSuccess
    {
        let opaquePointer = secureItemValue?.toOpaque()

        let secureItemValueData = Unmanaged<NSData>.fromOpaque(opaquePointer!).takeUnretainedValue()

        // Use secureItemValueData...
    }

但是,SecItemCopyMatching 声明在 Xcode 7 beta 4 中发生了变化:

旧:func SecItemCopyMatching(_ query: CFDictionary, _ result: UnsafeMutablePointer<AnyObject?>) -> OSStatus

新:func SecItemCopyMatching(_ query: CFDictionary!, _ result: UnsafeMutablePointer<Unmanaged<AnyObject>?>) -> OSStatus

...现在 secureItemValue 类型不匹配。

之前提取结果的机制令人困惑,我希望新声明在某种程度上更容易,但我不知道如何为 secureItemValue 变量声明正确的类型并提取结果。

这适用于 Xcode 7 beta 4

var dataTypeRef: AnyObject?

    let status: OSStatus = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(keychainQuery as CFDictionaryRef, UnsafeMutablePointer([=10=])) }

    if status == noErr {
        return dataTypeRef as? NSData
    }
    else {
        return nil
    }

根据 你可以删除 AnyObject 周围的 Unmanaged<>:

var secureItemValue: AnyObject?

let statusCode: OSStatus = SecItemCopyMatching(keychainItemQuery, &secureItemValue)