Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer casting with ARC

ARC is giving me a hard time with following cast:

NSDictionary *attributes;
SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes);

Error: Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef ' (aka 'const void *') is disallowed with ARC

like image 708
mmvie Avatar asked Oct 19 '11 14:10

mmvie


2 Answers

The problem is that attributes shouldn't be a dictionary, it should be a SecKeyRef or CFDataRef. And then cast that back into NSData for the password data copied into it.

Like so:

CFDataRef attributes;
SecItemCopyMatching((__bridge CFDictionaryRef)keychainItemQuery, (CFTypeRef *)&attributes);
NSData *passDat = (__bridge_transfer NSData *)attributes;
like image 64
utahwithak Avatar answered Sep 22 '22 15:09

utahwithak


As we were doing something similar things and using the example above, we were facing another problem:

CFDataRef resultRef;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary,
               (CFTypeRef *)&resultRef);
NSData* result = (__bridge_transfer NSData*)resultRef; 

This will result in an EXEC_BAD_ACCESS, because resultRef is not set to any adress and points somewhere to the memory.

CFDataRef resultRef = nil;

This will fix the error.

like image 6
scrat84 Avatar answered Sep 21 '22 15:09

scrat84