This really has nothing to do with MPMediaItemsCollection specifically. Rather, it's just the behavior of NSArray.
As the documentation for -[NSArray indexOfObject:]
states, it "returns the lowest index whose corresponding array value is equal to a given object." In this case, equality is determined by sending each object an -isEqual:
message.
So, if you have an array:
NSArray *array = @[@"A", @"B", @"C", @"A"];
You'll see the following:
[array indexOfObject:@"A"] // returns 0
[array indexOfObject:array[3]] // also returns 0
[array indexOfObject:[array lastObject]] // also returns 0
Essentially, -[NSArray indexOfObject]
looks like this:
- (void)indexOfObject:(id)object
{
for (NSUInteger i=0; i<[self count]; i++) {
if ([self[i] isEqual:object]) return i;
}
return NSNotFound;
}
If you want to know the indexes for all matching objects in the array, use -[NSArray indexesOfObjectsPassingTest:]
NSArray *array = @[@"A", @"B", @"C", @"A"];
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj isEqual:@"A"];
}];
NSLog(@"Indexes: %@", indexes);
>>Indexes: <NSIndexSet: 0x7fbc9b40b170>[number of indexes: 2 (in 2 ranges), indexes: (0 3)]
If you really need to know which index should be associated with your use of a non-unique item in the playlist, you'll have to keep track of that yourself by storing it when you retrieve the media item in the first place.