Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: CFTypeRef disallowed with ARC

I'd like get username/password out of my keychain. for this I followed this guide:

Simple iPhone Keychain Access

But this part is not allowed with ARC:

NSData *result = nil;    
OSStatus status = SecItemCopyMatching(
                  (CFDictionaryRef)searchDictionary,                                            
                  (CFTypeRef *)&result);

What can I do?

like image 807
filou Avatar asked Apr 20 '12 20:04

filou


2 Answers

ARC only manages Objective-C types. If you cast to Core Foundation types you have to tell ARC who owns the variable by using __bridge, __bridge_retained or __bridge_transfer.

Here's Apple's official documentation on toll-free bridging under ARC, or see this blog post (scroll down to Toll-Free Bridging) for a great overview.

For example:

NSData *inData = nil;
CFTypeRef inTypeRef = (__bridge CFTypeRef)inData;
OSStatus status = SecItemCopyMatching(
                   (__bridge CFDictionaryRef)searchDictionary, 
                   &inTypeRef);
like image 51
Simon Whitaker Avatar answered Sep 30 '22 18:09

Simon Whitaker


CFTypeRef inData = NULL;
OSStatus status = SecItemCopyMatching(
                   (__bridge CFDictionaryRef)searchDictionary, 
                   & inData);
NSData *data = (__bridge NSData *)inData;
like image 26
leeliang Avatar answered Sep 30 '22 20:09

leeliang