views:

224

answers:

4

When I print the value of a CFString constant e.g. an AddressBook label in the console, the output value is _$!<home>!$_. How do I extract only the pure value, e.g home? ANy help would be greatly appreciated please.

+5  A: 

Not all string constants have all that gubbins around them - it just happens that someone decided that the address book ones do. Therefore, I don't think there's any built-in function to remove them.

That makes this problem a simple "How do I remove characters?" question. There are many solutions to this, but a simple one is:

NSString *label = @"_$!<home>!$_";

NSString *rawLabel = [[label stringByReplacingOccurrencesOfString:@"_$!<" withString:@""] 
     stringByReplacingOccurrencesOfString:@">!$_" withString:@""];
iKenndac
Thank you. I just assumed it would be a feature of all constants rather than an AB quirk.
Run Loop
+6  A: 

If you're displaying the name of the property in your interface, use the ABCopyLocalizedPropertyOrLabel function or the ABPersonCopyLocalizedPropertyName function instead of attempting to extract a name from the private implementation-detail value of the constant.

If you're doing something like this:

NSLog(@"%@: %@", kABURLsProperty, URLsValue);

Try this instead:

//In a header
#define STRING_FROM_NAME(name) @#name

//In the implementation
NSLog(@"%@: %@", STRING_FROM_NAME(kABURLsProperty), URLsValue);

This will print the name of the constant, which is much more recognizable, instead of the private implementation-detail value of the constant.

(Note that, since this is a preprocessor macro, it doesn't follow variables. It just creates a string from whatever text you pass as the first argument; it does not attempt to reverse-lookup a string. So, if you pass a variable name, you will get the name of your variable in the output.)

Peter Hosey
A: 

Here the code snipped I use because ABCopyLocalizedPropertyOrLabel is Mac OS X only at the moment:

NSString *labelTranslation = @"";
if ([label isEqualToString:@"_$!<Home>!$_"])
 labelTranslation = @"Home";
else if ([label isEqualToString:@"_$!<Mobile>!$_"])
 labelTranslation = @"Mobile";
else if ([label isEqualToString:@"_$!<Work>!$_"])
 labelTranslation = @"Work";
else if ([label isEqualToString:@"_$!<WorkFAX>!$_"])
 labelTranslation = @"Work (Fax)";
else if ([label isEqualToString:@"_$!<Main>!$_"])
 labelTranslation = @"Main";
else if ([label isEqualToString:@"_$!<HomeFAX>!$_"])
 labelTranslation = @"Home (Fax)";
else if ([label isEqualToString:@"_$!<Pager>!$_"])
 labelTranslation = @"Pager";
else if ([label isEqualToString:@"_$!<Other>!$_"])
 labelTranslation = @"Other";
catlan
On the iPhone, try ABPersonCopyLocalizedPropertyName instead.
Peter Hosey
Nice Peter, deleting 16 lines of code, yeah! :)
catlan
A: 

Use:

NSString label = (NSString)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(contact, index));

-- Javier Vásquez

Javier Vásquez