ABAddressBook将值存储在NSDictionary中 [英] ABAddressBook store values in NSDictionary
问题描述
我有一个在UITableView
中显示ABAddressBook
个联系人的应用程序.目前,我正在将联系人读入NSDictionary
,但是对于某些用户来说,这似乎崩溃了,我怀疑这是内存问题.
I have an app that displays ABAddressBook
contacts in a UITableView
. Currently I'm reading the contacts into an NSDictionary
, however this appears to crash for some users, which I suspect is a memory issue.
是否有另一种方法可以在UITableView
中显示ABAddressBook
个联系人,而无需先将它们存储在NSDictionary
中或使用ABPeoplePicker
?
Is there another approach to display ABAddressBook
contacts in a UITableView
without either first storing them in an NSDictionary
or using ABPeoplePicker
?
推荐答案
您可以使用以下方式,
ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *arrTemp = (NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);
以上两行将为您在iPhone上的所有联系人创建一个数组.
The above 2 lines will create an array for all your contacts on the iPhone.
现在,您要显示的联系人的任何属性都可以使用以下代码显示.例如,我要显示所有联系人的名字,然后创建一个名为arrContact
的Mutable数组.
Now whatever property of a contact you want to display you can display by using the below code. For example, I want to display the first name of all contacts and then create one Mutable array called it arrContact
.
NSMutableArray *arrContact = [[NSMutableArray alloc] init];
for (int i = 0; i < [arrTemp count]; i++)
{
NSMutableDictionary *dicContact = [[NSMutableDictionary alloc] init];
NSString *str = (NSString *) ABRecordCopyValue([arrTemp objectAtIndex:i], kABPersonFirstNameProperty);
@try
{
[dicContact setObject:str forKey:@"name"];
}
@catch (NSException * e) {
[dicContact release];
continue;
}
[arrContact addObject:dicContact];
[dicContact release];
}
现在只需使用arrContact
数组在表视图中显示它即可.
Now just display it using the arrContact
array in a table view..
这篇关于ABAddressBook将值存储在NSDictionary中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!