NSDictionary`description`格式问题 - 像char数据一样处理结构 [英] NSDictionary `description` formatting problem -- treats structure like char data

查看:190
本文介绍了NSDictionary`description`格式问题 - 像char数据一样处理结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的类(类似于NSArray的概念,希望格式化的外观)有一个 description 格式化程序。当格式化程序的输出本身被打印(NSLog)时,它看起来很好,但是当它作为NSDictionary description的一个元素被包含时 / code> NSDictionary格式化程序似乎决定它是一个字符串,而不是一个结构定义,将其包含在引号中,并转义字符串中的所有控制字符。


$ b $当然,对于一个标准的NSArray来说,它并不是这样做的,所以我想知道它是如何决定如何处理字符串和其他字符串的。



例如,不是输出如下所示:

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

看起来像:

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

任何人都可以提出一种方法来改变这个行为?
$ b $ FWIW,我累积了描述字符串(数据主要包含调用结果到 NSDictionary description )在一个NSMutableString中,然后在出口执行 NSString stringFromString (尽管我不知道那是干什么用的)。

新增了



我想我找到了埋在NSDictionary中的答案(还没有检查过):


讨论

返回的NSString对象包含每个字典条目
的字符串表示。 descriptionWithLocale:indent:
获得给定键或值的字符串表示形式,如下所示:

 如果对象是NSString对象,它按原样使用。 

如果对象响应descriptionWithLocale:indent:,那

方法是调用以获取对象的字符串表示形式。

pre $ 如果对象响应descriptionWithLocale:,则该方法为


被调用来获得对象的字符串表示。

 如果没有满足上述条件,则通过调用对象的字符串

它的描述方法。



更新



好的,原来他们在撒谎。我实现了descriptionWithLocale:indent:它永远不会被调用。

更新9/23



有趣的是,如果我使我的类NSArray的子​​类,然后NSDictionary调用descriptionWithLocale:indent:和格式正确。听起来像NSDictionary是作弊和测试 isKindOfClass 而不是 respondsToSelector ,否则只是对非NS的东西偏见。

然而,要获得NSArray的子​​类是一种丑陋的行为,尽管它获得了许多我不想模仿的行为,并带有额外的未使用的数据。

etc



另一个选项是将转义的字符串转换回原来的字符串。这需要一个31行的程序来处理基础知识( \\\
\ t \ \\ ),而不需要子类NSArray。不利的一面是,这个例程必须插入​​任何可以显示我的类的NSLog调用中。另一个小缺点是转义字符串被我无法消除的引号字符包装,但是这几乎不可见。



明白了#ifdefed现在 - 不知道我会选择。

解决方案

这个问题的真正答案(OS X的安全功能似乎不会影响iOS控制台的写入),但有两个解决方法:

<有趣的是,如果我让我的类成为NSArray的子​​类,那么NSDictionary调用descriptionWithLocale:indent:并且它的格式正确,听起来像NSDictionary是作弊,测试是KindOfClass而不是响应oSelector,否则只是对非NS的东西产生偏见。

然而,从NSArray的子​​类来说,这样做很丑陋,但是从获取很多行为不想模仿,并携带额外的未使用的数据。


#2:另一种方法是将转义字符串转换回原来的字符串。这需要一个31行的过程来处理基本的(\ n,\ t,\和\)。另一方面,我不需要NSArray的子​​类,主要缺点是这个例程必须插入​​任何可以显示我的类的NSLog调用。另一个小缺点是逃逸的字符串被引用字符包装,我不能消除,但是这几乎没有注意到。



(接受这个答案,即使它不是真正的答案,因为我接受的%会受到其他的影响,我想问太多的难题。)


I've got a custom class (which resembles an NSArray in concept and, hopefully, formatted appearance) which has a description formatter.

When the output of the formatter is printed (NSLog) by itself it looks fine, but when it's included as an element of an NSDictionary description the NSDictionary formatter seems to decide that it's a character string, not a structure definition, encloses it in quotes, and escapes all of the control characters in the string.

It doesn't do this for a standard NSArray, of course, so I'm wondering how it decides to treat the string one way vs the other.

Eg, rather than having output that looks like:

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

It looks like:

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

Can anyone suggest a way to alter this behavior?

FWIW, I accumulate the description string (with data consisting primarily of the results from calls to NSDictionary description) in an NSMutableString, then do NSString stringFromString at the exit (though I don't know that that does anything).

Added

I think I found the answer (haven't checked yet) buried in the NSDictionary writeup:

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.

Update

Well, turns out they lie. I implemented descriptionWithLocale:indent: and it never gets called.

Update 9/23

Interestingly, if I make my class a subclass of NSArray then NSDictionary calls descriptionWithLocale:indent: and it formats correctly. Sounds like NSDictionary is "cheating" and testing isKindOfClass rather than respondsToSelector, or else is just prejudiced against non-NS stuff.

It's kind of ugly to have to subclass NSArray, though, in terms of acquiring a lot of behaviors I don't want to mimic, and carrying extra unused data.

Etc

Another option is to convert the escaped string back to its original. This takes a 31-line procedure to handle the basics (\n, \t, \", and \\). The up-side is that I don't need to subclass NSArray. The main downside is that this routine must be inserted in any NSLog call that could display my class. Another minor downside is that the escaped strings were wrappered with quote characters I can't eliminate, but that's hardly noticeable.

Got it #ifdefed for now -- not sure which I'll pick.

解决方案

There's no real answer to this question (the "security feature" of OS X doesn't appear to affect iOS console writes), but there are these two work-arounds:

#1: Interestingly, if I make my class a subclass of NSArray then NSDictionary calls descriptionWithLocale:indent: and it formats correctly. Sounds like NSDictionary is "cheating" and testing isKindOfClass rather than respondsToSelector, or else is just prejudiced against non-NS stuff.

It's kind of ugly to have to subclass NSArray, though, in terms of acquiring a lot of behaviors I don't want to mimic, and carrying extra unused data. Etc

#2: Another option is to convert the escaped string back to its original. This takes a 31-line procedure to handle the basics (\n, \t, \", and \). The up-side is that I don't need to subclass NSArray. The main downside is that this routine must be inserted in any NSLog call that could display my class. Another minor downside is that the escaped strings were wrappered with quote characters I can't eliminate, but that's hardly noticeable.

(Accepted this answer, even though it's not the "real" answer, because my accepted % would suffer otherwise. I ask too many difficult questions, I guess.)

这篇关于NSDictionary`description`格式问题 - 像char数据一样处理结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆