将通讯录联系人存储到nsdictionary中 [英] storing addressbook contacts into a nsdictionary
本文介绍了将通讯录联系人存储到nsdictionary中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我仍在尝试使用NSDictionaries来解决问题,并且遇到了我认为需要使用NSDictionaries的情况。本质上,我想将与每个联系人关联的所有电话号码存储在字典中。到目前为止,我有这个:
I'm still trying to wrap my head around using NSDictionaries, and have come into a situation where I believe I need to use one. essentially, I would like to store all the phone numbers associated with each contact into a dictionary. so far I have this:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (id person in thePeople)
{
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* name = (NSString *)ABRecordCopyCompositeName(person);
for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
{
NSString *phone = [(NSString *)ABMultiValueCopyValueAtIndex(phones,i) autorelease];
}
}
我想知道如何使用nsdictionary来存储每个人员,然后是与该人员相关的每个电话值的数组。
I was wondering how to use a nsdictionary to store each person, and then an array of each phone value that's associated with that person.
推荐答案
您要做什么?
您可以将所有姓名和电话号码放入这样的plist中:
You can put all names and phonenumbers into a plist like this:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray* allPeoplesDicts = [NSMutableArray array];
for (id person in thePeople)
{
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* name = (NSString *)ABRecordCopyCompositeName(person);
NSMutableArray* phones = [[NSMutableArray alloc] init];
for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
{
NSString *phone = [(NSString *)ABMultiValueCopyValueAtIndex(phones,i) autorelease];
[phones addObject:phone];
}
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,@"Name",phones,@"PhoneNumbers",nil];
[phones release];
[allPeoplesDicts addObject:personDict];
[personDict release];
}
这篇关于将通讯录联系人存储到nsdictionary中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文