0

I'm trying to get just the first 100 of contacts from the Address Book. What I've done is getting all the contacts and then tried to get only the first 100. For some reason that doesn't work (code below).

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);

NSRange theRange;
theRange.location = 0;
theRange.length = 100;

CFArrayRef allContactsNew = (CFArrayRef)[(NSMutableArray *)allContacts subarrayWithRange:theRange];//This gets an error

Would appreciate help here. Also, If you know any other method to get only the first 100 or so directly from the Address Book that could be very helpful.

Idan
  • 9,880
  • 10
  • 47
  • 76

1 Answers1

1

It worked correctly when I made these changes:

theRange.length = MIN(100, CFArrayGetCount(allContacts)); //avoid array out of bounds errors

CFArrayRef allContactsNew = CFBridgingRetain([(NSArray *)CFBridgingRelease(allContacts) subarrayWithRange:theRange]); //Add CFBridging functions recommended by Xcode
SSteve
  • 10,550
  • 5
  • 46
  • 72
  • Works perfectly, One thing that should be mentioned that this line releases "allContacts". Thank you! – Idan Aug 03 '12 at 21:26
  • I have to say that I'm not familiar with the CFBridgingRetain/CFBridgingRelease stuff. I just put them in because the program wouldn't compile otherwise and Xcode's fix-it put them in for me. That could be because I had ARC on and you don't. If your problem was the array bounds issue, you should decide if the CFBridgingRetain/CFBridgingRelease statements are appropriate for you. – SSteve Aug 03 '12 at 22:45
  • Bounds weren't the issue. Anyway, Thanks. – Idan Aug 04 '12 at 02:06