我在ARC环境中进行类型转换时遇到问题,如果有人愿意解决的话:

当我使用这一行代码时:

NSData *resultData = nil;

NSMutableDictionary *passwordQuery = [query mutableCopy];

[passwordQuery setObject: (id) kCFBooleanTrue forKey: (__bridge  id) kSecReturnData];

status = SecItemCopyMatching((__bridge  CFDictionaryRef) passwordQuery, (CFTypeRef *) &resultData);

然后我收到一个错误:
Cast of an indirect pointer to an Objective C pointer to 'CFTypeRef*'(aka 'const void **')is disallowed with ARC.
请建议我任何解决此问题的方法。

提前致谢..

最佳答案

直到调用CFTypeRef之后,结果数据类型才是SecItemCopyMatching,因此首先传入CFTypeRef:

CFTypeRef resultData = nil;
status = SecItemCopyMatching((__bridge CFDictionaryRef) passwordQuery,  &resultData);

由于查询指定resultData应该是CFDataRef,因此resultData现在是CFDataRef,您现在可以将其像这样转换。然后将其进一步转换为NSData
CFDataRef resultCFData = (CFDataRef)resultData;
NSData *resultNSData = (__bridge NSData *)resultCFData;

或一行:
NSData *resultNSData = (__bridge NSData *)(CFDataRef)resultData;

08-05 08:21