xcode 在 iPhone 上调试时 NSLog 将 unicode 字符输出为垃圾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSLog outputs unicode characters as garbage when debugging on the iPhone
提问by Dmitry Sokurenko
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.
编辑:NSLog 输出在模拟器中运行良好,但在连接到真实设备时不起作用。它似乎是一个错误 - http://openradar.appspot.com/11148883。碰巧它与 LLDB 相关,将 Xcode 切换到 GDB 可以解决问题。JetBrain 的 AppCode 可以与 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
我在应用程序中有一堆 unicode 字符串,如果我尝试使用NSLog(@"%@", aString) 之类的东西输出这些字符串中的任何一个,那么字符串中的所有 ASCII 字符都将被正确打印,但所有的西里尔字母字母会被弄乱,而不是
newLocation: coordinate:60.019584,30.284954 'Удельная'
I'm getting:
我越来越:
newLocation: coordinate:60.019584,30.284954 '–ü–?–∫–a–?–Ω–Ω–?–≥–?—?—?–∫–∞—è'
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 与 unicode 字符一起工作?而且我只在寻找某种单行解决方案,我知道有一些方法可以编写半页代码并输出 unicode 字符,但我正在寻找更短的东西。理想情况下,我正在寻找 NSString 的一些方法,这将使一切正常。例如
NSLog(@"%@", [aString someThingThatMakesUnicodeWorkWithXcodeConsole]);
采纳答案by Hailei
As far as I know it is relevant to NSLog() and LLDB on some Xcode versions. Have a try with one of these solutions:
据我所知,它与某些 Xcode 版本上的 NSLog() 和 LLDB 相关。尝试使用以下解决方案之一:
- 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.
- 检查登录 Xcode Organizer >> Devices >> your device >> Console。
- 如果您使用的是后者,请使用 GDB 而不是 LLDB 作为您的调试器。这可以从架构选项中更改。请参考下方“cocos2d man”评论中的步骤。
- 升级到 Xcode 4.3.2。有人说它解决了这个问题,但我自己还没有证实。
回答by Anatoliy Gatt
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:
是的,显然您可以创建一个包含和输出西里尔字母的字符串。当我学习 Objective-C 时,一开始我也遇到了同样的问题(我也在使用俄语单词之类的东西)。所以解决方案是将字符串转换为其他格式,如下所示:
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.
PS 这意味着您需要将字符串创建为 C 样式字符串,并将其编码参数设置为 4(UTF-8)。您可以在 NSString 中 NSStringEncoding 的文档中查看所有可用参数列表。
回答by Ranga
Try to convert it in to UTF8 string.
尝试将其转换为 UTF8 字符串。
NSString *str = [aString UTF8String]
NSLog(@"%@", str);
Hope this helps.
希望这可以帮助。
回答by Naloiko Eugene
Try this. It works for me.
尝试这个。这个对我有用。
NSLog(@"%@", [NSString stringWithCString:[[places description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
回答by Akshay
Try putting it like NSLog(@"%@", aString);
试着把它像 NSLog(@"%@", aString);
EDIT :
编辑 :
you can convert it in UTF8 string. This could get you through.
您可以将其转换为 UTF8 字符串。这可以让你通过。
NSString *str = [aString UTF8String];
Hope this helps.
希望这可以帮助。