带 \0 的 NSString
启动包含 @"\0" 的 NSString 的最佳方法是什么?
NSString* foo = @"bar\0";
导致“CFString 文字包含 NUL 字符”警告。
What is the best way to initiate NSString that contains @"\0"?
NSString* foo = @"bar\0";
causes 'CFString literal contains NUL character' warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSString
对象用于文本。如果您希望在文本数据中包含非文本数据(例如,当要将其写入套接字或保存到一个文件)。NSString
objects are for text. Consider using anNSData
orNSMutableData
object if you wish to have non-text data in amongst textual data (for example, when it is to be written to a socket or saved to a file).您可以将 NUL 字符放入
NSString
实例中,这是一个示例:请注意,在应用执行过程中的令人惊讶的时刻,该字符串将被转换为 C 字符串(或类似的东西)以与较低级别的 API 交互,这将导致字符串被截断。例如,上述程序的输出是这样的:
显示
NSString
本身完好无损,但NSLog()
无法正确显示它。You can put a NUL character into an
NSString
instance, here's one example:Be aware that at surprising points in your app's execution, the string will be converted into a C string (or something similar) for interacting with lower-level API, and that this will cause the string to be truncated. As an example, the output of the above program is this:
Showing that the
NSString
itself is completely intact but thatNSLog()
won't display it correctly.