iosobjective-cnsmutablearraycompareabrecordref

How to check if the NSMutableArray already has (__bridge id) object in the array


I am trying to add ABRecordRef item into my NSMutableArray. Just learned that It's needed to cast the C TypeDef into Objective-C id type using (_bridge id). So, before adding new items into the array, I want to check if the object is already in the array. Therefore, i tried using [nsmutablearray containsObject] but it does not seem to be working. Duplicate items still get added into the array. Do you know what could be wrong here?

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    if (![_buddiesList containsObject:(__bridge id)person]) {
        [_buddiesList addObject:(__bridge id)person];
        NSLog(@"Added");
    }
    return NO;
}

Solution

  • There is no NS-equivalent to ABRecordRef and -containsObject: simply calls -isEqual: on all its objects to determine if there is a duplicate already included or not, so your approach can't work.

    I suggest writing a wrapper class around ABRecordRef and implementing your own -isEqual: method there.

    Update:
    As pointed out by @omz, it does work, because CFEqual() is called, thanks! Using a wrapper class around ABRecordRef is still a good idea, though.