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

时间:2019-05-14 05:53:22

标签: ios xcode casting automatic-ref-counting

我收到此编译器错误:

  

非目标C指针类型'void *的隐式转换   ARC不允许_Nullable'到'NSObject * __ strong *'

当我尝试编译在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的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果您不知道它是std::map还是NSDictionary *,则正确的类型是NSArray *

id