http://blog.cnrainbird.com/index.php/2012/07/19/object-c_zi_ding_yi_dui_xiang_nslog_shu_ru_you_yong_xin_xi/

继续昨天的故事关于Object-C面向对象开发
自定义完对象以后,想查看对象的信息,于是我们直接NSLog:

NSLog(@"%@",_LocalVideo);

结果输出的是一个内存地址,这肯定不是我们想要的。

2012-07-19 09:37:46.933 umiwiForTest[2463:17803] <LocalVideo: 0x7cc3a9b0>

之前的时候有被同事问到这种情况怎么能输出这个对象的所有信息,我还一本正经的说自已写个循环挨个输出呗,哎,基础不牢害死人。今天自已遇到这个问题了。google一下,很快找到了答案:在自定义的类里实现- (NSString *)description;这个方法就可以了。

#pragma mark - description
- (NSString *)description{
  return [NSString stringWithFormat:
          @"{\n    title = %@,\n subtitle = %@\n}",
          self.title,self.subTitle];
}

运行后的输出

2012-07-19 10:07:13.326 umiwiForTest[2951:17803] {
    title = 邓峰:双向交流做好了,才能提高执行力,
 subtitle = 时长: 00:02:35  点播: 4163日期: 2010-05-07 17:18:00
}


上面看着还不错,可是当我把这个类搁到一个数组里,然后输出数组的时候,还是乱了。

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}"
)

作为有洁癖的人(看到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.

上面说,要么重新NSArray重新实现description,要么去掉这些特殊字符的转义。想想为了输全一个自已写的类要重新NSArray有点大题小作,而去掉这些特殊字符的转义也不是我想要的,因为我就是格式化出来,方便查看嘛,罢了,罢了,看来是无解,就此搁下。

参考资料:
NSLog – Making an NSObject return useful info
Overriding NSObject Description Method
Print Instances of NSObject’s Sub-class Friendly By Overriding NSObject Description Method
Auto Description Category for NSObject (any object)
NSDictionary `description` formatting problem — treats structure like char data

 

 

 

posted on 2015-10-17 23:05  lushengcao  阅读(286)  评论(0编辑  收藏  举报