title: CFArrayGetValueAtIndex returning non-ABRecordRef
body: <p>I've been having a crash on some user's iPhones and I finally got someone who I can replicate the problem with.  Here's the key segment of the code</p>
  
  <pre><code>ABAddressBookRef addressbook = ABAddressBookCreate();
  if( addressbook )
  {
      //Got this via http://stackoverflow.com/questions/4641229/code-example-for-abaddressbookcopyarrayofallpeopleinsourcewithsortordering
      ABRecordRef source = ABAddressBookCopyDefaultSource(addressbook);
      CFArrayRef sortedPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressbook, source, kABPersonSortByFirstName);
      //Sort them first
  
      if( sortedPeople )
      {
          CFIndex contactCount = ABAddressBookGetPersonCount(addressbook);
  
          for( int i = 0; i&lt;contactCount; i++ )
          {
              ABRecordRef ref = CFArrayGetValueAtIndex(sortedPeople, i);
              NSMutableString *fName = [[[NSMutableString alloc] init] autorelease];
              NSMutableString *lName = [[[NSMutableString alloc] init] autorelease];
              NSMutableDictionary *identifiers = [[[NSMutableDictionary alloc]init]autorelease];
              if( ref )
              {
                  //Get the user's name first
                  NSLog(@"%@ is the reference", ref);
                  CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
                  if( firstName )
                  {
                      NSString *fn = [NSString stringWithFormat:@"%@",firstName];
                      if([fn hasPrefix:@"(null"])
                          [fName appendString:@""];
                      else
                      {
                          [fName appendString:[NSString stringWithFormat:@"%@", firstName]];
                          [fName setString:[fName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[fName substringToIndex:1]uppercaseString]]];
                      }
                      CFRelease(firstName);
                  }
             }
         }
     }
  }
  </code></pre>
  
  <p>Apparently the result of this line of code: 
      ABRecordRef ref = CFArrayGetValueAtIndex(sortedPeople, i);</p>
  
  <p>Is sometimes coming back as an NSCFType instead of ABPerson.  Any idea on how to check the type of the result?  I'm trying to prevent this from crashing the user's phone.  Once I get to this line:</p>
  
  <pre><code> ABRecordCopyValue(ref, kABPersonFirstNameProperty);
  </code></pre>
  
  <p>I get an EXC_BAD_ACCESS on this line.  And it happens when the log file looks like this:</p>
  
  <pre><code>2011-09-11 17:24:31.355 Holler[1345:707] &lt;CPRecord: 0x6642fb0 ABPerson&gt; is the reference
  2011-09-11 17:24:31.358 Holler[1345:707] &lt;CPRecord: 0x66431d0 ABPerson&gt; is the reference
  2011-09-11 17:24:31.361 Holler[1345:707] &lt;CPRecord: 0x66433b0 ABPerson&gt; is the reference
  2011-09-11 17:24:31.365 Holler[1345:707] &lt;CPRecord: 0x6640fd0 ABPerson&gt; is the reference
  2011-09-11 17:24:31.369 Holler[1345:707] &lt;CPRecord: 0x6643510 ABPerson&gt; is the reference
  2011-09-11 17:24:31.372 Holler[1345:707] __NSCFType is the reference
  </code></pre>
  
  <p>Any help would be much appreciated!!!</p>