我成功添加" firstName"," lastName"," phone"来自NSMutableDictionary的地址簿联系方式。
现在我想在表格视图中显示它:" firstName" +" lastName"但我们可以访问此联系人的号码。也许我必须创建NSObject?
这是我的代码:
- (void)getContacts:(ABAddressBookRef)addressBook
{
NSArray *allData = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger contactCount = [allData count];
for (int i = 0; i < contactCount; i++) {
ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)allData, i);
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
if (firstName) {
dictionary[@"firstName"] = firstName;
}
if (lastName) {
dictionary[@"lastName"] = lastName;
}
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phones);
if (phoneNumberCount > 0) {
NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, 0));
dictionary[@"phone"] = phone;
}
// or if you wanted to iterate through all of them, you could do something like:
//
// for (int j = 0; j < phoneNumberCount; j++) {
// NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j));
//
// // do something with `phone`
// }
if (phones) {
CFRelease(phones);
}
self.test = [dictionary allValues];
NSLog(@"%@", self.test);
}
}
当我记录self.test(NSArray)时,我有类似的东西:
2015-07-30 00:00:51.842 ChillN[4764:59116] (
John,
"888-555-5512",
Appleseed
)
2015-07-30 00:00:51.843 ChillN[4764:59116] (
Anna,
"555-522-8243",
Haro
)
我想在我的手机上显示:&#34; John Appleseed&#34;,&#34; Anna Haro&#34;但仍然存储此联系人的号码。
我不知道下一步该做什么......