The reason your containsObject:
call is failing to report the duplicate is that NSArray
uses the NSObject
method -isEqual:
to determine equality, and CF types such as ABRecordRef
do not have this method. We need to find a better way to determine equality.
The most efficient way to do this is to use an Objective-C wrapper around the AddressBook APIs, transforming each record into a bona fide NSObject
instead of just bridge-casting it. Jim Dovey's iPhoneContacts project is a well-written solution for this. To build an equality testing method on top of this, just add an isEqual:
method that uses the technique shown below for comparing the record IDs.
Alternatively, you can keep things as they are, but do a linear scan over the array each time you're about to add a record. Something like this:
BOOL foundMatch = NO;
for(id buddy in self.buddiesList)
{
if(ABRecordGetRecordID(buddy) == ABRecordGetRecordID(person))
{
foundMatch = YES;
break;
}
}