Get label names for ABPerson properties

独自空忆成欢 提交于 2019-12-12 04:19:24

问题


I want to get all label names for properties of ABPerson object. For example: record ABPerson has three phone numbers defined: Mobile, Other, Work. I use labelAtIndex method to get label name but returned string contains needed value wrapped in characters $!!$. Instead of returning just "Mobile" I get these "_$!<" wrap-characters.

I have following code:

//person object points to ABPerson record from addressBook
ABMultiValue *phoneNumbers = [person valueForProperty:kABPhoneProperty];

NSUInteger count = [phoneNumbers count];

for (int i = 0; i < count; i++) {
    NSLog(@"Phone numbers label: %@ value: %@", [phoneNumbers labelAtIndex:i], [phoneNumbers valueAtIndex:i]);        
}

In log I get following:

2012-01-23 01:14:04.234 FixMyAddressBook[3667:707] Phone numbers label: _$!<Mobile>!$_ value: +327382738273
2012-01-23 01:14:04.370 FixMyAddressBook[3667:707] Phone numbers label: _$!<Work>!$_ value: +3293829328

Could someone point me please how can I get label names for properties without special characters?


回答1:


As far as I'm aware you need to get the localized label for that item, you'll need to make sure you're using the right reference code.

// Grab the right property first
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers);

for(int k = 0; k < phoneNumberCount; k++)
        {

        //Get phone number label by iterating across this
            CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );

CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
            CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );    
// converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
//do whatever you want to do here
//release your references
        CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberValue);
}


来源:https://stackoverflow.com/questions/8965608/get-label-names-for-abperson-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!