0

How can I get the index of the addressbook when we know the ABRecordRef and address book?

I am using:

ABRecordRef ref= CFArrayGetValueAtIndex(AddressBookPeople, index);

to get the record reference at a given index.

Similarly, how do I get the index of the record when the ABRecordRef value is known?

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Gobi M
  • 3,243
  • 5
  • 32
  • 47

2 Answers2

0

NSArray has a method:

- (NSUInteger)indexOfObject:(id)anObject 

that returns the lowest index whose corresponding array value is equal to a given object.

CFArray is toll-free bridged with NSArray. So I think this should work,

int index = [(NSArray *)AddressBookPeople indexOfObject:ref];

EDIT:::

  ABAddressBookRef addressBook = ABAddressBookCreate();
  CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
  ABRecordRef ref= CFArrayGetValueAtIndex(people, 4);
  NSUInteger i = [(__bridge NSArray *)people indexOfObject:(__bridge id)(ref)];

This returned me right index.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • here AddressBookPeople is CFArrayRef, This doesn't have indexOfObject method – Gobi M Sep 10 '13 at 11:04
  • Thats why I said that CFArrayRef is tollfree bridged with NSArray so it should work. You may need to typecast though. See my edit. – Puneet Sharma Sep 10 '13 at 11:06
  • No its returned 20000+ record index 600 records only available – Gobi M Sep 10 '13 at 11:54
  • people = ABAddressBookCopyArrayOfAllPeople(addressBook); NSArray *tempPeople = (__bridge NSArray *)(AddressBookPeople); NSUInteger i = [tempPeople indexOfObject:(__bridge id)(ref)]; – Gobi M Sep 10 '13 at 11:55
  • So I got curious and tried it my self. See my edit. It is working for me. – Puneet Sharma Sep 10 '13 at 12:48
0

You can also more properly use:

CFArrayGetFirstIndexOfValue
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71