How to call SecItemCopyMatching in Xcode 7 beta 4?

∥☆過路亽.° 提交于 2019-12-04 11:34:09

问题


In previous versions of Xcode 6 and 7 with Swift, this syntax would work:

    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...
    }

However, the SecItemCopyMatching declaration has changed in Xcode 7 beta 4:

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

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

...and now the secureItemValue type does not match.

The mechanism was confusing before to extract the result, and I'm hoping it is somehow easier with the new declaration, but I don't know how to declare the correct type for the secureItemValue variable and extract the result.


回答1:


This works on Xcode 7 beta 4

var dataTypeRef: AnyObject?

    let status: OSStatus = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(keychainQuery as CFDictionaryRef, UnsafeMutablePointer($0)) }

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



回答2:


According to this answer you can just remove Unmanaged<> around AnyObject:

var secureItemValue: AnyObject?

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


来源:https://stackoverflow.com/questions/31549059/how-to-call-secitemcopymatching-in-xcode-7-beta-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!