今天自已遇到这个问题了。google一下,很快找到了答案:在自定义的类里实现- (NSString *)description;这个方法就可以了。
可是当我把这个类搁到一个数组里,然后输出数组的时候,还是乱了。
2012-07-19 10:06:22.595 umiwiForTest[2927:17803] (
"{\n title = \U9093\U5cf0\Uff1a\U53cc\U5411\U4ea4\U6d41\U505a\U597d\U4e86\Uff0c\U624d\U80fd\U63d0\U9ad8\U6267\U884c\U529b,\n subtitle = \U65f6\U957f: 00:02:35 \U70b9\U64ad: 4163\U65e5\U671f: 2010-05-07 17:18:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}"
)
"{\n title = \U9093\U5cf0\Uff1a\U53cc\U5411\U4ea4\U6d41\U505a\U597d\U4e86\Uff0c\U624d\U80fd\U63d0\U9ad8\U6267\U884c\U529b,\n subtitle = \U65f6\U957f: 00:02:35 \U70b9\U64ad: 4163\U65e5\U671f: 2010-05-07 17:18:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}",
"{\n title = \U674e\U5f00\U590d\Uff1a\U79fb\U52a8\U4e92\U8054\U7f51\U5728\U519c\U6751\U7684\U53d1\U5c55,\n subtitle = \U65f6\U957f: 00:02:14 \U70b9\U64ad: 12064\U65e5\U671f: 2010-04-16 12:42:00\n}"
)
作为有洁癖的人(看到xcode有Warning就要解决掉),这种实在看着不爽,于是查资料找到一个解决办法:
#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.
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.
上面说,要么重新NSArray重新实现description,要么去掉这些特殊字符的转义。想想为了输全一个自已写的类要重新NSArray有点大题小作,而去掉这些特殊字符的转义也不是我想要的,因为我就是格式化出来,方便查看嘛,罢了,罢了,看来是无解,就此搁下。