Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLog outputs unicode characters as garbage when debugging on the iPhone

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]);
like image 784
Dmitry Sokurenko Avatar asked Apr 19 '12 11:04

Dmitry Sokurenko


2 Answers

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.

like image 52
Anatoliy Gatt Avatar answered Oct 02 '22 01:10

Anatoliy Gatt


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.
like image 31
Hailei Avatar answered Oct 02 '22 01:10

Hailei