Question

Did ask a question about the same problem here: Silly problem with UTF8 but somewhat screwed up the thing so i do a new question and try to to it cleaner.

I do apologize if i ask this again.

I have been trying to get this to work but just can't get the result i want. I have tried the recommendations from my previous thread but still cant get it to work. As you understand i am very new at this and have really tried to read up on it.

Here is the scenario:

  • NSMutableArray
  • Store Swedish characters: "ÅÄÖ"
  • NSLog the array
  • Output is not correct

Code:

NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject: @"==="];
[myArray addObject: @"ÅÄÖ"];
[myArray addObject: @"==="];
NSLog(@"myArray: %@", myArray);

The output i get is:

2010-11-10 21:20:17.826 nsmutablearray_test[2976:207] myArray: (
"===",
"\U00c5\U00c4\U00d6",
"==="
)

The output i am looking for is:

"===",
"ÅÄÖ",
"==="

As i have tested all kinds of things for this (simple) problem i would really appreciate if someone nice could show me the code to fix this?

Regards

Was it helpful?

Solution

Here's a simple solution for you:

NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject: @"==="];
[myArray addObject: @"ÅÄÖ"];
[myArray addObject: @"==="];
NSLog(@"myArray: %@", myArray);

for (NSString *string in myArray) {
    NSLog(@"%@", string);
}

Output:

Swedish[3453:a0f] ===
Swedish[3453:a0f] ÅÄÖ
Swedish[3453:a0f] ===

OTHER TIPS

\U00c5 is the unicode code for Å, so technically, that output is correct. The string is still the same in memory, it's just that NSLog isn't outputting it how you're expecting. But it's still the same string.

This is just a problem with how NSLog handles UTF8 if I remember correctly. Try storing the description into an NSString first and then logging.

Either way, the data is correct, this is purely an NSLog issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top