Below is an example containing two arrays that return different indices for their lastObject.
NSMutableArray *ary0 = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:[NSDecimalNumber decimalNumberWithString:@"2"],[NSDecimalNumber one], nil]];
NSLog(@"%d",[ary0 indexOfObject:[ary0 lastObject]]);
Logs 1, as expected.
NSMutableArray *ary1 = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:[NSDecimalNumber one],[NSDecimalNumber one], nil]];
NSLog(@"%d",[ary1 indexOfObject:[ary1 lastObject]]);
Logs 0.
I do not see how the index of the lastObject in ary1 is 0, even if there are two identical objects in ary1. Can someone please explain why this makes sense or point out the very silly mistake I’m making? Thanks!
You’re not getting the index of the last object, you’re getting the index of the first object that is equal to the last object. indexOfObject: is documented as searching from the first element of the array to the last, stopping when it finds a match.