我想获取ABPersonRef和ABGroupRef的所有属性的列表,而不必使用kABPersonFirstNameProperty,kABPersonLastNameProperty的iOS预定义键...一个特定的人。我知道有预定义的键,但是Apple将来很可能会添加新的键,所以我想做些类似的事情:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < [allPeople count]; ++i) {
ABRecordRef person = [allPeople objectAtIndex:i];
// This is the line that I can't figure out.
NSArray *allProperties = (NSArray *)ABRecordCopyArrayOfAllProperties(person);
}
我知道我将遇到多值项,稍后我将不得不循环这些多值项,但是目标是获得一个可以迭代用于单值属性的键列表。我不在乎返回的类是什么,NSArray,NSDictionary ...等等。
我非常感谢任何建议!
最佳答案
您可以尝试以下操作:
使用ARC :
NSDictionary* dictionaryRepresentationForABPerson(ABRecordRef person)
{
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
for ( int32_t propertyIndex = kABPersonFirstNameProperty; propertyIndex <= kABPersonSocialProfileProperty; propertyIndex ++ )
{
NSString* propertyName = CFBridgingRelease(ABPersonCopyLocalizedPropertyName(propertyIndex));
id value = CFBridgingRelease(ABRecordCopyValue(person, propertyIndex));
if ( value )
[dictionary setObject:value forKey:propertyName];
}
return dictionary;
}
只要
propertyName
不成为UNKNOWN_PROPERTY
,遍历属性的数量也许是有意义的