I'am trying to seperate a string with danish characters into a NSMutableArray. But something is not working. :(
My code:
NSString *danishString = @"æøå";
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[danishString length]];
for (int i=0; i < [danishString length]; i++)
{
NSString *ichar = [NSString stringWithFormat:@"%c", [danishString characterAtIndex:i ]];
[characters addObject:ichar];
}
If I do at NSLog on the danishString it works (returns æøå);
But if I do a NSLog on the characters (the array) I get some very stange characters - What is wrong?
/Morten
First of all, your code is incorrect.
characterAtIndex
returnsunichar
, so you should use@"%C"
(uppercase) as the format specifier.Even with the correct format specifier, your code is unsafe, and strictly speaking, still incorrect, because not all unicode characters can be represented by a single
unichar
. You should always handle unicode strings per substring:You should definitely read String Programming Guide.
Finally, the correct code for you:
If with
NSLog(@"%@", characters);
you see "strange character" of the form "\Uxxxx", that's correct. It's the default stringification behavior ofNSArray
bydescription
method. You can print these unicode characters one by one if you want to see the "normal characters":