Question

I am trying to use a custom logger to give clean console output for a text-based game. Below is the code of the method in question:

void MyLog(NSString *format, ...)
{
    va_list args;
    va_start(args, format);
    NSString *formattedString = [[NSString alloc] initWithFormat:format arguments: args];
    va_end(args);
    [[NSFileHandle fileHandleWithStandardOutput] writeData:[formattedString dataUsingEncoding: NSNEXTSTEPStringEncoding]];
    [formattedString release];
}

and here is this example's use of it:

NSLog(@"Unicode text: \u2665");
NSLog(@"ASCII Text");
MyLog(@"Unicode text: \u2665");
MyLog(@"ASCII text");

and the output:

2012-04-26 00:08:53.614 TextCraft[11319:a0f] Unicode text: ♥
2012-04-26 00:08:53.617 TextCraft[11319:a0f] ASCII Text
ASCII text

As you can see, the MyLog function works fine for low-page characters, but when it tries to print high-page characters, it simply skips the whole line. Does anyone know why this is?

Was it helpful?

Solution

You're using NSNEXTSTEPStringEncoding in your call to -dataUsingEncoding:, which is just ASCII plus some additions. If you want to support Unicode, you should use an appropriate encoding like NSUTF8StringEncoding.

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