iOS:在数组中保存字符串数据

iOS:在数组中保存字符串数据,ios,objective-c,abaddressbook,Ios,Objective C,Abaddressbook,我正在开发联系人列表应用程序,以便我需要将联系人数据和存储在tableview中,我做了如下操作 - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook { NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook); contactList= CFBridgingRelease(ABAddressBookCopyArr

我正在开发联系人列表应用程序,以便我需要将联系人数据和存储在tableview中,我做了如下操作

     - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
   {
     NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

for (int i = 0; i < numberOfPeople; i++)
{
    ABRecordRef person = (__bridge ABRecordRef)contactList[i];

   firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
 lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
    {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        NSLog(@"  phone is:%@", phoneNumber);
    }
         CFRelease(phoneNumbers);

           }


        }
-(void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSInteger numberOfPeople=ABAddressBookGetPersonCount(地址簿);
contactList=CfBrigingRelease(abAddressBookCopyArrayFallPeople(addressBook));
for(int i=0;iNSString*phoneNumber=CfBrigingRelease(ABMultiValueCopyValueAtIndex(phoneNumber,i));
NSLog(@“电话号码:%@”,电话号码);
}
CFRelease(电话号码);
}
}

目标获取名和姓的字符串格式的数据。我的问题是我需要将名和姓中的数据保存到数组中,但该数组位于for循环的外侧。因此,我将把数组数据传递到表视图。

1。选项1:只需做一件事,在for循环内部创建NSMutableDictionary。将所有(名字、姓氏等)存储在其中。在for循环的最后,将其添加到数组中。 2.选项2:将联系人创建为具有所需属性的对象。请参见此处的示例

-(void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSInteger numberOfPeople=ABAddressBookGetPersonCount(地址簿);
NSMutableArray*contactList=CfBrigingRelease(abAddressBookCopyArrayFallPeople(addressBook));
NSMutableArray*tableviewArray=[[NSMutableArray alloc]init];//添加此行
for(int i=0;iNSString*phoneNumber=CfBrigingRelease(ABMultiValueCopyValueAtIndex(phoneNumber,i));
NSLog(@“电话号码:%@”,电话号码);
}
[tableviewArray addObject:[NSDictionary Dictionary WithObjectsSandKeys:firstName、@“first_name”、lastName、@“last_name”、nil];//添加此行
CFRelease(电话号码);
}
}

正如您所给出的,tableview数组仅适用于该循环,如果数组来自该循环,则返回空值。如果数据以不同的方法保存,我需要保持数据不变,那么电话号码呢?提前谢谢。请帮助。创建一个全局数组,但目标是获取电话号码,仅用于最后的索引名。这意味着仅显示las名称的电话号码并非全部请让我知道String*phoneNumber=CfBrigingRelease(ABMultiValueCopyValueAtIndex(phoneNumber,NumberOfPhoneNumber-1));[tableviewArray addObject:[NSDictionary Dictionary WithObjectsSandKeys:firstName、@“first_name”、lastName、@“last_name”、phoneNumber、@“phone_no”、nil];//添加这行kk谢谢你它成功了,我们可以使用搜索栏过滤名称吗?&否你能给我举个例子吗?我试了很多次,但找不到id
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{

    NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    NSMutableArray * contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

    NSMutableArray *tableviewArray =[[NSMutableArray alloc] init]; // ADD This Line

    for (int i = 0; i < numberOfPeople; i++)
    {
        ABRecordRef person = (__bridge ABRecordRef)contactList[i];

        NSString *  firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *  lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);




        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
        {
            NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
            NSLog(@"  phone is:%@", phoneNumber);
        }

        [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , nil]]; // ADD This Line

        CFRelease(phoneNumbers);

    }


}