iphone - NSDictionary `description` 格式化问题 - 将结构视为 char 数据

标签 iphone objective-c ios formatting

我有一个自定义类(它在概念上类似于 NSArray,并且希望是格式化​​的外观),它有一个 description 格式化程序。

当格​​式化程序的输出本身被打印(NSLog)时,它看起来很好,但是当它作为 NSDictionary description 的元素包含时,NSDictionary 格式化程序似乎决定它是一个字符串,不是结构定义,将其括在引号中,并转义字符串中的所有控制字符。

当然,对于标准的 NSArray,它不会这样做,所以我想知道它是如何决定以一种方式处理字符串而不是另一种方式的。

例如,而不是输出看起来像:

theChildren = (
                  {
                      "@meta.type" = "ns3:location";
                      "childNumber" = 0;
                      ...

看起来像:

theChildren = "( \n\t\t {\n\t  \"@meta.type\" = \"ns3:location\";\n\t   \"childNumber\" = 0;
...

谁能提出改变这种行为的方法?

FWIW,我在 NSMutableString 中累积描述字符串(数据主要由调用 NSDictionary description 的结果组成),然后在导出处执行 NSString stringFromString (虽然我不知道那有什么作用)。

添加

我想我在 NSDictionary 的文章中找到了答案(尚未检查):

Discussion

The returned NSString object contains the string representations of each of the dictionary’s entries. descriptionWithLocale:indent: obtains the string representation of a given key or value as follows:

If the object is an NSString object, it is used as is.

If the object responds to descriptionWithLocale:indent:, that

method is invoked to obtain the object’s string representation.

If the object responds to descriptionWithLocale:, that method is

invoked to obtain the object’s string representation.

If none of the above conditions is met, the object’s string

representation is obtained by invoking its description method.

更新

好吧,事实证明他们在撒谎。我实现了 descriptionWithLocale:indent: 并且它永远不会被调用。

9 月 23 日更新

有趣的是,如果我让我的类成为 NSArray 的子类,那么 NSDictionary 会调用 descriptionWithLocale:indent: 并且它的格式正确。听起来 NSDictionary 是在“作弊”并测试 isKindOfClass 而不是 respondsToSelector,否则只是对非 NS 的东西有偏见。

但是,在获取许多我不想模仿的行为以及携带额外未使用的数据方面,必须将 NSArray 子类化有点丑陋。

另一种选择是将转义的字符串转换回其原始字符串。这需要一个 31 行的过程来处理基础知识(\n\t\"\\)。好处是我不需要继承 NSArray。主要的缺点是这个例程必须插入​​到任何可以显示我的类的 NSLog 调用中。另一个小缺点是转义的字符串被包装了引号字符我无法消除,但这几乎不引人注意。

现在知道#ifdefed -- 不确定我会选择哪个。

最佳答案

这是在 OS X 10.5+ 版本的 syslog() 中引入的一个很棒的与安全相关的“功能”。

正如 Apple 工程师在这篇文章中所解释的:Who broke NSLog on Leopard? ,

That's the behavior of syslog(). From the man page:

Newlines and other non-printable characters embedded in the message string are printed in an alternate format. This prevents someone from using non-printable characters to construct misleading log messages in an output file. Newlines are printed as "\n", tabs are printed as "\t". Other control characters are printed using a caret ("^") representation, for example "^M" for carriage return.

NSLog() 写入的 ASL 子系统也是如此(至少在 豹)。将 XML 写入文件是一种合理的选择。

Chris Kane cocoa 框架,Apple

man syslog了解更多信息。

关于iphone - NSDictionary `description` 格式化问题 - 将结构视为 char 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7521683/

相关文章:

iphone - 如何仅在按下 View 顶部的按钮后才开始运行 View ?

iphone - Objective-C (iPhone) ivars 和内存管理

objective-c - 从框架伞头中排除内部头

ios - 如何将语音转换为文本 iPhone?

iphone - 即使在ARC中也可能存在对象泄漏?

ios - 在 ios 中创建自定义进度指示器

几个月后 iPhone 应用程序崩溃

objective-c - 将可变数量的参数传递给选择器

ios - 显示前加载ViewController的WebView

iphone - 限制要显示的表格单元格数量