user-44651
user-44651

Reputation: 4124

Converting Unicode text into ASCII adds extra characters

I have this raw string 101\U2013113 W 4th St.

Which should read 101-113 W 4th St

This string comes from a CLPlacemark

placemark.addressDictionary[@"Street"]

However, when I decode it with:

NSData *decode = [placemark.addressDictionary[@"Street"] dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];
NSString * address =  [[NSString alloc] initWithData:decode encoding:NSASCIIStringEncoding];

It outputs some extra characters at the beginning of the string that I can't paste here but an NSLog shows "\U00ff\U00fe101\^S 113 W 4th St Chattanooga TN 37402";

If I NSLog the placemark, it reads as it should 101-113 W 4th St

What am I doing wrong?

Upvotes: -1

Views: 61

Answers (1)

matt
matt

Reputation: 535988

What am I doing wrong?

Everything. addressDictionary is deprecated, and there is no need to decode any data in any case; the address information already comes to you as a string. (It's unclear what a "raw" string is supposed to be; a string is a string.) You cannot convert the string to ASCII, as it may (and here does) contains nonASCII characters. Just use the string you're given.

A CLPlacemark has properties such as subthoroughfare (a house number) and thoroughfare (a street name), or if you want the address as a whole, get its postalAddress and format with a CNPostalAddressFormatter (you'll need to import the Contacts framework).

Upvotes: 2

Related Questions