iOS: CFTypeRef disallowed with ARC
Asked Answered
H

2

8

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?

Hesitancy answered 20/4, 2012 at 20:8 Comment(0)
P
21

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);
Piacular answered 20/4, 2012 at 20:19 Comment(3)
thank you simon. I already used __bridge but for (CFTypeRef *)&result); it didn't work.. Xcode noticed: Incompatible types casting 'NSData *__strong *' to 'CFTypeRef *' (aka 'const void **') with a __bridge castHesitancy
Separate out your cast from your dereference. Something like this: CFTypeRef r = (__bridge CFTypeRef)result; OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &r);Piacular
didn't work. leelang solution for some reason did. xcode 7.3Loux
W
1
CFTypeRef inData = NULL;
OSStatus status = SecItemCopyMatching(
                   (__bridge CFDictionaryRef)searchDictionary, 
                   & inData);
NSData *data = (__bridge NSData *)inData;
Wordless answered 2/12, 2015 at 10:3 Comment(1)
"Try this" is not very useful since it doesn't explain what was wrong of how to fix it in another case.Ellerd

© 2022 - 2024 — McMap. All rights reserved.