2012-05-07 47 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]); 
     } 
    } 
}