1

This little gem cost me a couple of hours of debugging so I thought I'd post the problem (and solution) here. I'm creating an Array of address book constants (kABPerson*Property) that I later use for extracting contact data. Surprisingly, the values all appear to be zero. It looks like the backing for these are actually global variables even though they look like constants (on iOS 7 anyway), but I haven't been able to track down any memory overwrite problems. What the heck could be going on?

Ethan
  • 486
  • 1
  • 6
  • 15

1 Answers1

0

It turns out that the kABPerson*Property "constants" are actually initialized on the first call to ABAddressBookCreate. Before that the values are all zero. Here is some sample code:

    NSLog(@"Before AddressBook Create, kABPersonLastNameProperty = %d", kABPersonLastNameProperty);
    ABAddressBookRef store = ABAddressBookCreateWithOptions(NULL, NULL);
    NSLog(@"After AddressBook Create, kABPersonLastNameProperty = %d", kABPersonLastNameProperty);

If you haven't accessed the address book before, you would see output like:

    Before AddressBook Create, kABPersonLastNameProperty = 0
    After AddressBook Create, kABPersonLastNameProperty = 1

I Other calls may initialize the constants as well but from my exploration a call to ABAddressBookGetAuthorizationStatus is not sufficient.

Ethan
  • 486
  • 1
  • 6
  • 15
  • is this still the case? I used a break point in the first line of `appdidfinishlaunching` and `po kABPersonPhoneMobileLabel` returns the right thing. – bogardon Sep 17 '14 at 06:07
  • I still seem the same behavior on iOS 8 Simulator. I copied and pasted the sample lines to the top of my `didFinishLaunching`. Let me know if you get different results! – Ethan Sep 17 '14 at 19:53