尝试将指针转换为 ARC 下的指针

Trying to cast a pointer to a pointer under ARC

我遇到这个编译器错误:

Implicit conversion of a non-Objective-C pointer type 'void * _Nullable' to 'NSObject *__strong *' is disallowed with ARC

当我尝试编译这段在 ARC 之前有效的旧代码时。实际代码在一个由数组驱动的循环中。此片段只是一个循环价值的代码,用于突出显示没有无关内容的错误。

- (void)myCode
{
    NSDictionary *sDict; // I want the @{ @"key" : @"value" } to end up here

    // this is in a loop to set several dictionaries
    // vcValue is set from a controlling array, but ends up like this:
    NSValue *vcValue = [NSValue valueWithPointer:&sDict];
    NSObject **vcPointer = [vcValue pointerValue]; // <<-- generates the error
    *vcPointer = @{ @"key" : @"value" }; // for sDict
}

我正在使用 NSObject,因为根据控制数组,指针可能指向 NSDictionary 或 NSArray。在这个例子中,它指向一个字典。

定义 vcPointer 的正确方法是什么?

如果您不知道它是 NSDictionary * 还是 NSArray *,正确的类型是 id

NSDictionary *sDict;
NSValue *vcValue = [NSValue valueWithPointer:&sDict];
id __strong *vcPointer = (id __strong *)vcValue.pointerValue;
*vcPointer = @{ @"key": @"value" };