Imagine the following code:
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@200, @100, nil];
NSInteger n1 = (NSInteger)[arr objectAtIndex:0];
NSInteger n2 = (NSInteger)[arr objectAtIndex:1];
NSInteger r = n1 + n2;
NSLog(@"n: %lid", r);
The result I am getting is: -4309586476825365866d, the expected one is: 300.
I also tried to evaluate the individual expressions in debugger to check what I am getting when reading the values from the array, like this: po (NSInteger)[arr objectAtIndex:0]. This showed the correct number, yet when trying to sum them both: po (NSInteger)[arr objectAtIndex:0] + (NSInteger)[arr objectAtIndex:0], an invalid result is generated.
Any ideas why? I am very new to Objective-C, so any source where I could get more info about the issue is appreciated.
NSArraycan only hold objects: namelyNSNumbers.-objectAtIndex:returns the pointer to the number; casting it to anNSIntegerisn't what you want to do. Instead, use[[arr objectAtIndex:0] integerValue];