How to change NSString encoding?

I have an NSArray from NStrings, I got this from NSLog when printing an array. Here is the code I implemented:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; ..... NSArray *queryResults = [[query results] copy]; for (NSMetadataItem *item in queryResults) { id value = [item valueForAttribute: kMDItemAlbum]; [databaseArray addObject: value]; } "The Chronicles Of Narnia: Prince Caspian", "Taste the First Love", "Once (Original Soundtrack)", "430 West Presents Detroit Calling", "O\U0308\U00d0\U00b9u\U0301\U00b0\U00aeA\U0300O\U0308A\U0300O\U0308I\U0301A\U030a-O\U0301a\U0300A\U0302\U00a1", "\U7ea2\U96e8\U6d41\U884c\U7f51", "I\U0300\U00ab\U00bc\U00abO\U0303A\U030aE\U0300y\U0301\U00b7a\U0301", "A\U0303n\U0303\U00b8e\U0300\U00b2I\U0300C\U0327U\U0300", "\U00bb\U00b3A\U0308i\U0302O\U0303\U00bdO\U0301N\U0303", "American IV (The Man Comes Aro", "All That We Needed", 

Now, how can I change human-readable strings for human-readable strings? Thanks.

+4
source share
3 answers

Looking at the escaping performed by the description (for example, \U0308 ), the lines are incorrect (for example, "ร–รยนรบ ยฐ ยฎร€ร–ร€ร–รร…-ร“ร ร‚ยก") because the data you received was incorrect.

This may not be a Spotlight bug. (You can verify this by trying another ID3 tag library.) Most likely, the files themselves contain poorly encoded tags.

To fix this:

  • Encode it in 8-bit character encoding. You can't just pick the encoding (like "ASCII", which Cocoa maps to ISO Latin 1 the last time I checked) at random; you need to use an encoding that contains all the input characters and correctly encodes them for what you are going to do next. Try ISO Latin 1, ISO Latin 9, Windows 1252 encoding, and MacRoman in that order.
  • Decode encoded data as UTF-8. If this fails, return to step 1 and try using a different encoding.

If step 2 succeeds in any attempt, this is your valid data (if you are not very lucky). If it does not work during all attempts, the data cannot be restored, and you can warn the user that their input files contain dummy tags.

+2
source

These lines are encoded by utf-8. You can decode them:

 NSString *myDecoded = [NSString stringWithUTF8String:myEscapedString]; 

So, to process the full array of 'completeArray', you can first convert to const char * and then back to NSString:

 NSMutableArray *processed = [NSMutableArray arrayWithCapacity:completeArray.count]; for (NSString* s in completeArray) { [processed addObject:[NSString stringWithUTF8String:[s cStringUsingEncoding:ASCIIEncoding]]]; } 
+1
source

Parsing these lines is not very simple: see this publication for background. He received links to other SO posts with specific solutions to this problem.

+1
source

All Articles