16
votes

EDIT: NSLog output works well in the simulator, but doesn't work when connected to a real device. And it seems that it is a bug — http://openradar.appspot.com/11148883. Also it happens that it is related to the LLDB, switching Xcode to GDB resolves the problem. Either it's possible to JetBrain's AppCode, which works well with the LLDB.


I have a bunch of unicode strings in the application, and if I try to output any of those strings using something like NSLog(@"%@", aString) then all the ASCII characters in the string will be printed fine but all the cyrillic letters will be messed up, so instead of

newLocation: coordinate:60.019584,30.284954 'Удельная'

I'm getting:

newLocation: coordinate:60.019584,30.284954 '–ü–æ–∫–ª–æ–Ω–Ω–æ–≥–æ—Ä—Å–∫–∞—è'

And that's quite hard to do any debugging with that kind of output. And because that app is targeted for the Russian market only I can't just change locale and use English strings.

So I wonder if there any way to make NSLog work well with unicode characters? And I'm looking only for some kind of one-liner solution, I know that there are some ways to write half a page of code and output unicode chars, but I'm looking for something shorter. Ideally I'm looking for some method of NSString that will make it all work. e.g.

NSLog(@"%@", [aString someThingThatMakesUnicodeWorkWithXcodeConsole]);
5
Are you sure that your aString is correctly storing the response? From what you posted I believe the aString didn't store the response correctly, so yes when you do print it with NSLog it will fail. I print greek characters with NSLog all the time. How are you storing the response to the aString ?Lefteris
Where do the strings come from? Do read them from a file? When the are inside the code it should work. I tested: NSString *s = @"Удельная"; NSLog(@"%@", s); and it worked well.Kai Huppmann
Those data are coming from a file, but a string literal haven't worked for me either. But it seems that I've found the source — it works well in the simulator, but outputs garbage when connected to the iPhone, so probably something is just wrong with some settings. Thank you.Dmitry Sokurenko

5 Answers

4
votes

Yes, obviously you can create a string that will contain and output cyrillic letters. When I was learning Objective-C, I had the same problem in the begining(I'm as well was working with Russian words and stuff like that). So solution is to convert the string to other format like this:

NSString *string = [NSString stringWithCString:"Привет, как дела?" encoding:4];
NSLog(@"%@", string);

or

NSString *string = [NSString stringWithUTF8String:"Этот вариант короче!"];
NSLog(@"%@", string);

Hope it helps you!

P.S It means that you need to make create your strings as C-Style Strings, and set their encoding parameter to 4(UTF-8). You can see all list of avaliable parameters in the documentation to NSStringEncoding in NSString.

3
votes

As far as I know it is relevant to NSLog() and LLDB on some Xcode versions. Have a try with one of these solutions:

  • Check log in Xcode Organizer >> Devices >> your device >> Console.
  • Use GDB as your debugger instead of LLDB if you are using the latter one. This can be changed from the schema options. Please refer to the steps in the comment by "cocos2d man" below.
  • Upgrade to Xcode 4.3.2. Some people say it solved this issue, but I haven't confirmed this myself.
1
votes

Try to convert it in to UTF8 string.

NSString *str = [aString UTF8String]
 NSLog(@"%@", str);

Hope this helps.

0
votes

Try putting it like NSLog(@"%@", aString);

EDIT :

you can convert it in UTF8 string. This could get you through.

NSString *str = [aString UTF8String];

Hope this helps.

0
votes

Try this. It works for me.

NSLog(@"%@", [NSString stringWithCString:[[places description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);