NSLog 和 -打印描述 - 使用 NSString 和 UTF-8 之间的区别
我很困惑。我正在解析 json 字符串。
在解析之前,我检查 NSString
的内容是什么。
在 Xode4 中: 当我点击 NSString
变量“打印描述”时 控制台将值显示为 UTF-8 的 \u434 \u433
格式
当我调用 NSLog("%@",content)
时,控制台显示“可读”字符UTF-8 编码。
为什么会有所不同?我怎么知道我要解析的字符串是 100% UTF-8 ?
谢谢。
I'm confused. I'm parsing json string.
Before parsing, I check what is the content of the NSString
.
In Xode4
:
When I click on the NSString
variable "print description"
The console show the value as \u434 \u433
format of the UTF-8
When I call NSLog("%@",content)
the console show the "readble" character of the UTF-8 encoding.
Why is this different? How can I know that the string I got to parse is 100% UTF-8 ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以通过 any 方法看到要查找的西里尔字符,而不是转义字符,那么您正在使用 UTF-8 字符串。
“-description”方法不是您想要在此处使用的方法。它更有可能显示转义字符;特别是,任何时候您在 NSArray 或 NSDictionary 等属性列表项中存储值时,其 -description 通常都会转义除纯 ASCII 之外的任何字符。
NSLog 是一个更可靠的指南,因为它不使用 -description。如果它出现在 NSLog 中,则可能没问题。
如果您想绝对确定您的字符串是否正确编码为 UTF-8,测试它的最佳方法就是显示它。在用户界面中创建一个文本界面元素(NSTextField 或 UITextField),将其连接起来,并将字符串设置为值。如果显示在那里,则格式正确。
简短版本:如果它在调试器中显示为转义字符,并不一定意味着它不是 UTF8。如果它以正确的字符显示在任何地方(包括 NSLog),则它可能采用正确的编码。如果您想确定,请设置一个测试界面元素并查看它的外观。
If you can see the Cyrillic characters you're looking for, rather than the escapes, through any method, then you're working with a UTF-8 string.
The "-description" method is not what you want to use here. It's more likely to show escaped characters; in particular, any time you store a value in a property list item like an NSArray or NSDictionary, its -description will generally escape any characters other than plain ASCII.
NSLog is a more reliable guide, because it doesn't use -description. If it's showing up in NSLog, it's probably just fine.
If you want to be absolutely sure your string is properly encoded UTF-8, the best way to test it is to display it. Create a text interface element (an NSTextField or UITextField) in your user interface, wire it up, and set your string as the value. If it displays there, it is properly formatted.
Short version: if it shows up in the debugger as escaped characters, it doesn't necessarily mean it's not UTF8. If it's showing up anywhere (including NSLog) with the proper characters, it's probably in the proper encoding. If you want to be sure, set up a test interface element and see how it looks there.