Pointer casting with ARC

别来无恙 提交于 2019-12-04 08:16:02

问题


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


回答1:


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;



回答2:


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.




回答3:


Need to change attributes to &attributes

CFDataRef attributes;
SecItemCopyMatching((__bridge CFDictionaryRef) keychainItemQuery,  ( CFTypeRef*) &attributes);
NSData* passDat=(__bridge_transfer NSData*) attributes;


来源:https://stackoverflow.com/questions/7822892/pointer-casting-with-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!