title: Iterating through contacts in ABAddressBookRef
body: <p>I'm trying to iterate through contacts (<code>ABRecordRef</code>'s) in the address book(<code>ABAddressBookRef</code>). The app crashes when trying to copy <code>kABPersonKindProperty</code> property of the ref to check whether its a person contact or a company contact.</p>
  
  <pre><code>ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
  ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
      if (granted) {
          ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
          CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
          CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
  
          NSMutableArray *contactsArray = [[NSMutableArray alloc] init];
          NSMutableArray *companyArray = [[NSMutableArray alloc] init];
  
          for( int i = 0 ; i &lt; nPeople ; i++ ) {
              ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
              if (ABRecordCopyValue(ref, kABPersonKindProperty) == kABPersonKindPerson) { // The app crashes on this line.
                  MNContact *contact = [[MNContact alloc] initWithRecordReference:ref];
                  [contactsArray addObject:contact];
              } else if (ABRecordCopyValue(ref, kABPersonKindProperty) == kABPersonKindOrganization) {
                  MNCompany *company = [[MNCompany alloc] initWithRecordReference:ref];
                  [companyArray addObject:company];
              }
          }
  
          self.hasLoadedContactsFromDevice = YES;
          _personContacts = [[NSArray alloc] initWithArray:contactsArray copyItems:YES];
          //            _companyContacts = [[NSArray alloc] initWithArray:companyArray copyItems:YES];
      } else {
          _personContacts = nil;
          _companyContacts= nil;
          self.hasLoadedContactsFromDevice = NO;
      }
      dispatch_semaphore_signal(self.sema);
  });
  dispatch_semaphore_wait(self.sema, DISPATCH_TIME_FOREVER);
  </code></pre>
  
  <p>If I print <code>ref</code> this is what I get </p>
  
  <pre><code>{
  kCGImageSourceUseHardwareAcceleration = 1;
  "kImageIOInfoHeader_alphaInfo" = 5;
  "kImageIOInfoHeader_bitsPerComponent" = 8;
  "kImageIOInfoHeader_bitsPerPixel" = 32;
  "kImageIOInfoHeader_cacheImageBlocks" = 0;
  "kImageIOInfoHeader_colorspaceModel" = 1;
  "kImageIOInfoHeader_imageHeight" = 96;
  "kImageIOInfoHeader_imageHeightOriginal" = 96;
  "kImageIOInfoHeader_imageIndex" = 0;
  "kImageIOInfoHeader_imageWidth" = 96;
  "kImageIOInfoHeader_imageWidthOriginal" = 96;
  "kImageIOInfoHeader_isLittleEndian" = 0;
  "kImageIOInfoHeader_plugin" = "&lt;CGImagePlus 0x165846f0&gt;{w=96 h=96 prov=0x0 props=0x16584110 metadata=0x16584760}";
  "kImageIOInfoHeader_pluginHandlesReMapping" = 1;
  "kImageIOInfoHeader_rowBytes" = 384;
  "kImageIOInfoHeader_session" = "&lt;CGImageReadSessionRef 0x165844f0&gt;{offset=3453 readref=0x165841f0}";
  "kImageIOInfoHeader_supportsMultipleResolutions" = 1;
  }
  </code></pre>
  
  <p>So my idea was to do a routine check whether the ref is an instance of class <code>ABRecordRef</code>.</p>
  
  <p>Can anyone tell me what I'm doing wrong? Any suggestions are welcome.</p>