将间接指针强制转换为 Objective-C 指针
Posted
技术标签:
【中文标题】将间接指针强制转换为 Objective-C 指针【英文标题】:cast of indirect pointer to an Objective-C pointer 【发布时间】:2012-09-13 18:27:31 【问题描述】:我有以下代码:
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary, (CFTypeRef *)&value);
导致错误:
Implicit conversion of an indirect pointer to an Objective-C pointer to 'CFTypeRef *' (aka 'const void **') is disallowed with ARC
关于如何解决这个问题的任何想法?我尝试将CFTypeRef *
更改为id *
,但没有成功
这是完整的方法:
+ (NSData *)keychainValueForKey:(NSString *)key
if (key == nil)
return nil;
NSMutableDictionary *attrDictionary = [self attributesDictionaryForKey:key];
// Only want one match
[attrDictionary setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
// Only one value being searched only need a bool to tell us if it was successful
[attrDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSData *value = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)attrDictionary, (CFTypeRef *)&value);
if (status != errSecSuccess)
DLog(@"KeychainUtils keychainValueForKey: - Error finding keychain value for key. Status code = %d", status);
return [value autorelease];
【问题讨论】:
【参考方案1】:您可以将其转换为 void *
:
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary,
(void *)&value);
如果您使用的是 Objective-C++,您可能需要转换两次:
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary,
(CFTypeRef *)(void *)&value);
或者你可以使用一个临时变量:
CFTypeRef cfValue = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary, &cfValue);
NSData *value = (__bridge id)cfValue;
【讨论】:
以上是关于将间接指针强制转换为 Objective-C 指针的主要内容,如果未能解决你的问题,请参考以下文章