2012-05-07 67 views
3

我使用此代碼來獲取聯繫人列表,然後我想將聯繫人列表存儲到一個數組中,好吧只是第一個名字,如果我得到如何添加所有的名字,然後我將繼續前進,其他屬性也是如此。在NSMutableArray中存儲聯繫人列表

- (void)viewDidLoad { 
    // Implement viewDidLoad if you need to do additional setup after loading the view. 
    [super viewDidLoad]; 

    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object 
    NSArray *abContactArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array 

    NSInteger totalContacts =[abContactArray count]; 

    for(NSUInteger loop= 0 ; loop < totalContacts; loop++) 
    { 
     ABRecordRef record = (ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record 
     // NSLog(@"%@,%@,%@",recordIdString,firstNameString,lastNameString); 

     [myarray addObject:firstNameString]; 
     NSLog(@"%@",[myarray objectAtIndex:1]); 

     if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group 
     { 
      ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record 

      recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id 

      firstNameString = (NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book 
      lastNameString = (NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book 
      //NSLog(@"%@,%@,%@",recordIdString,firstNameString,lastNameString); 
     } 
    } 
} 

myarray是在.h類中創建的對象NSMutableArray *myarray

任何幫助將不勝感激。謝謝。

+0

問題是什麼? – ssteinberg

回答

3

試試這個:

ABAddressBookRef addressBook = ABAddressBookCreate(); 
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);  

//The array in which the first names will be stored as NSStrings 
NSMutableArray *firstNames = [[NSMutableArray alloc] init]; 

for(NSUInteger index = 0; index <= ([arrayOfPeople count]-1); index++){ 

    ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];  
    NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty); 
    [firstNames addObject: currentFirstName]; 
} 

//OPTIONAL: The following line sorts the first names alphabetically 
NSArray *sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

我測試了這一點,它的工作原理。如果您對代碼有任何問題,請添加評論,然後我會盡力提供幫助。

0

剛剛從猜你的問題是什麼:改變你的代碼如下:

- (void)viewDidLoad { 
    // Implement viewDidLoad if you need to do additional setup after loading the view. 
    [super viewDidLoad]; 

    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object 
    NSArray *abContactArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array 

    NSInteger totalContacts =[abContactArray count]; 

    for(NSUInteger loop= 0 ; loop < totalContacts; loop++) 
    { 
     ABRecordRef record = (ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record 

     if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group 
     { 
      ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record 

      recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id 

      firstNameString = (NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book 
      lastNameString = (NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book 
      //NSLog(@"%@,%@,%@",recordIdString,firstNameString,lastNameString); 

      // changes took place here! 
      [myarray addObject:firstNameString]; 
      NSLog(@"%@",[myarray objectAtIndex:loop]); 
     } 
    } 
}