I'm stuck converting the Keychain query result using Swift.
My request seems to be working:
let queryAttributes = NSDictionary(objects: [kSecClassGenericPassword, "MyService", "MyAccount", true],
forKeys: [kSecClass, kSecAttrService, kSecAttrAccount, kSecReturnData])
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var dataTypeRef : Unmanaged<AnyObject>?
let status = SecItemCopyMatching(queryAttributes, &dataTypeRef);
let retrievedData : NSData = dataTypeRef!.takeRetainedValue() as NSData
*** ^^^^can't compile this line^^^^
})
My problem is, code won't compile:
Bitcast requires both operands to be pointer or neither
%114 = bitcast %objc_object* %113 to %PSs9AnyObject_, !dbg !487
Bitcast requires both operands to be pointer or neither
%115 = bitcast %PSs9AnyObject_ %114 to i8*, !dbg !487
LLVM ERROR: Broken function found, compilation aborted!
I don't know how to convert Unmanaged<AnyObject>
to NSData
.
Any ideas?
Looks like you have hit a compiler bug, which you should report. You can take a different path to retrieving the value, such as the following:
The error manifests itself when using
AnyObject
as a type parameterT
ofUnmanaged<T>
. The following code snippet compiles without issue, which uses a more specific type,CFError
:As the Keychain API returns a different result depending of the query attributes, the use of
AnyObject
is required.Use
withUnsafeMutablePointer
function andUnsafeMutablePointer
struct to retrieving the data, such as the following:it works fine with release (Fastest [-O]) build.
To get this to work you need to access the retained value for each keychain constant first. For example:
You'll then need to reference the constant you created in the keychain dictionary object like so.
I wrote a blog post about it at: http://rshelby.com/2014/08/using-swift-to-save-and-query-ios-keychain-in-xcode-beta-4/
Hope this helps!
rshelby