2012-05-07 3 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]); 
     } 
    } 
} 
関連する問題