English 中文(简体)
custom logging method MyLog not working for high-page characters in Objective-C
原标题:

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?

最佳回答

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.

问题回答

暂无回答




相关问题
Caching System.Console output

We have some methods that output a lot of coloured text to the Console. It s actually a menu that is built by looping through a collection of data. Because we are writing item by item (and one line ...

console app c++

i m new to console apps and would appreciate some pointers... i have created a new console app and (not finished but it should be working), i selected win32 console app and then selected empty ...

Java prints output twice in a very specific circumstance

System.out.print("My string: "); My string: BUILD SUCCESSFUL (total time: 1 second) System.out.print("My string "); My string BUILD SUCCESSFUL (total time: 1 second) System.out.print("My string: ...

Removing pause after reading using readLine in java

I am reading several lines from the console in a java program using the readLine command from the BufferedReader class. However, the program pauses at the end of the last line and does not proceed ...

热门标签