我有一个自定义类(在概念上类似于NSArray,希望格式化外观),它有一个description格式化程序.
当单独打印格式化程序的输出(NSLog)时,它看起来很好,但当它作为NSDictionary description的元素包含时,NSDictionary格式化程序似乎决定它是一个字符串,而不是结构定义,用引号括起来,转义字符串中的所有控制字符.
当然,对于标准的NSArray,它不会这样做,所以我想知道它是如何决定以一种方式对待另一种方式来处理字符串的.
例如,而不是具有如下输出:
theChildren = (
{
"@meta.type" = "ns3:location";
"childNumber" = 0;
...
Run Code Online (Sandbox Code Playgroud)
看起来像:
theChildren = "( \n\t\t {\n\t \"@meta.type\" = \"ns3:location\";\n\t \"childNumber\" = 0;
...
Run Code Online (Sandbox Code Playgroud)
任何人都可以提出改变这种行为的方法吗?
FWIW,我NSDictionary description在NSMutableString中累积描述字符串(主要包含来自调用的结果的数据),然后NSString stringFromString在退出处执行(虽然我不知道这会做什么).
我想我找到了NSDictionary写入中的答案(尚未检查):
讨论
返回的NSString对象包含每个字典条目的字符串表示形式.descriptionWithLocale:indent:获取给定键或值的字符串表示形式,如下所示:
Run Code Online (Sandbox Code Playgroud)If the object is an NSString object, it is used as is. If the object responds to descriptionWithLocale:indent:, that调用方法以获取对象的字符串表示形式.
Run Code Online (Sandbox Code Playgroud)If the object responds to descriptionWithLocale:, that method is调用以获取对象的字符串表示形式.
Run Code Online (Sandbox Code Playgroud)If none of the above conditions …