some days ago I've read a post written by another user regarding comparisons between NSNumber objects using the < (less) operator.. He was getting wrong results and people told him that it was comparing the addresses of the NSNumber objects instead of their values... I've tried to reproduce the problem but there is something I'm missing:
int main (int argc, char*argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *arrayDistance = [NSMutableArray array];
[arrayDistance addObject:
[NSNumber numberWithInteger: 10]];
[arrayDistance addObject:
[NSNumber numberWithInteger: 20]];
[arrayDistance addObject:
[NSNumber numberWithInteger: 8]];
[arrayDistance addObject:
[NSNumber numberWithInteger: 9]];
int iMinor = 0;
int i;
for (i = 0; i < [arrayDistance count]; i++) {
if([arrayDistance objectAtIndex: i] < [arrayDistance objectAtIndex: iMinor])
{
iMinor = i;
}
}
NSLog(@"iMinor is %d", iMinor);
[pool release];
return 0;
}
The code above correctly returns 2, why? Shouldn't it be comparing the addresses instead of the values of the NSNumber objects within the arrayDistance array? Shouldn't it be necessary to get the integer value out of the NSNumber objects or use the compare method of the NSNumber objects?
Does the NSNumber class overload the < (less) operator in order to compare the actual values stored within the objects? If so, where is it written in the reference documentation?
I hope the question is clear. Thank you in advance for any help.