如何在Xcode 7 beta 4中调用SecItemCopyMatching?
在以前的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... }
但是,Xcode 7 beta 4中的SecItemCopyMatching声明已经更改:
OLD: func SecItemCopyMatching(_ query: CFDictionary, _ result: UnsafeMutablePointer<AnyObject?>) -> OSStatus
NEW: func SecItemCopyMatching(_ query: CFDictionary!, _ result: UnsafeMutablePointer<Unmanaged<AnyObject>?>) -> OSStatus
…和现在的secureItemValuetypes不匹配。
该机制之前提取结果混淆,我希望它是在某种程度上更容易与新的声明,但我不知道如何声明正确的types为secureItemValuevariables,并提取结果。
这适用于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 }
根据这个答案,你可以删除Unmanaged<>
围绕AnyObject:
var secureItemValue: AnyObject? let statusCode: OSStatus = SecItemCopyMatching(keychainItemQuery, &secureItemValue)