指向Objective-C指针的间接指针的强制转换

adi*_*dit 10 iphone objective-c ipad ios

我有以下代码:

 OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary, (CFTypeRef *)&value);
Run Code Online (Sandbox Code Playgroud)

这导致错误:

Implicit conversion of an indirect pointer to an Objective-C pointer to 'CFTypeRef *' (aka 'const void **') is disallowed with ARC
Run Code Online (Sandbox Code Playgroud)

有关如何解决此问题的任何想法?我已经尝试过更改CFTypeRef *为a 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];
}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 30

你可以把它投射到void *:

OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary,
    (void *)&value);
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Objective-C++,则可能需要将其强制转换两次:

OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary,
    (CFTypeRef *)(void *)&value);
Run Code Online (Sandbox Code Playgroud)

或者您可以使用临时变量:

CFTypeRef cfValue = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attrDictionary, &cfValue);
NSData *value = (__bridge id)cfValue;
Run Code Online (Sandbox Code Playgroud)