iosnsvalue

NSValue getValue: method returns garbage value on iPhone 6 but correct value on iPhone 5


I am storing an NSInteger inside NSValue and while retrieving the value back I am getting a garbage value. The interesting part is that I am getting this garbage value on iPhone 6 and the correct one on iPhone 5.

Here is a part of my code:

NSValue *unknown = nil;

[self getValueForUnknown:&unknown];

if (unknown) {
    NSInteger unknownInt;
    [unknown getValue:&unknownInt];

    NSLog(@"%lu", (unsigned long)unknownInt);    // prints the correct value on iPhone 5 but a garbage value on iPhone 6
}

...

- (void)getValueForUnknown:(NSValue **)unknownArg {
    int x = 0;
    *unknownArg = [NSValue value:&x withObjCType:@encode(int)];
}

Note: Both the devices are running iOS 9.3.1


Solution

  • I referred to Apple Docs for the correct usage of NSValue and found this:

    enter image description here

    I was trying to get back the int value into an NSInteger variable and after declaring unknownInt as int, everything is working fine now.