Getting error using CFTypeRef with ARC

自古美人都是妖i 提交于 2019-12-24 14:15:14

问题


I basically followed this tutorial, and soon realized the project wouldn't compile because I was using ARC. I managed to suppress all the errors using __bridge (>.>) but I am still getting one error message, and I managed to read this stack question, but didn't understand how to apply the resolution to my problem.

Basically the method that is giving me the problem looks like this:

+ (NSString*)getPasswordForKey:(NSString*)aKey
{
 NSString *password = nil;

 NSMutableDictionary *searchDictionary = [self dictionaryForKey:aKey];

 [searchDictionary setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
 [searchDictionary setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];


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

 if (result)
 {
    password = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

 }
 return password;
} 

回答1:


I think you are making unnecessarily complex type casts by trying to cast the pointer-to-pointer argument. How about this:

CFTypeRef result = NULL;
BOOL statusCode = SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &result);
if (statusCode == errSecSuccess) {
    NSData *resultData = CFBridgingRelease(result);
    password = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
}


来源:https://stackoverflow.com/questions/11012860/getting-error-using-cftyperef-with-arc

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