Search code examples
iosxcodecastingautomatic-ref-counting

Trying to cast a pointer to a pointer under ARC


I'm getting this compiler error:

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

when I try to compile this old code that worked prior to ARC. The actual code is in a loop driven by an array. This snippet is just one loop-worth of the code to highlight the error with no extraneous stuff.

- (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
}

I'm using NSObject because the pointer might be to a NSDictionary or an NSArray, based on the controlling array. In this example, it points to a dictionary.

What is the proper way to define vcPointer?


Solution

  • If you don't know whether it's an NSDictionary * or an NSArray *, the correct type is id.

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