Object-C自定义对象NSLog输入信息
2015-10-17 23:05
337 查看
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
继续昨天的故事关于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
相关文章推荐
- 黑马程序员——ObjectiveC基础——NSString对象
- 黑马程序员——ObjectiveC基础——对象关系
- 黑马程序员——ObjectiveC基础——Self与Super
- [Albert的专栏]★★objective-c面向对象之——类的继承
- OC学习NSString与NSMutableString字符串
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- OC学习NSArray与NSMutableArray数组
- Objective-C中的instancetype和id关键字
- Objective-C:自定义Block函数
- Objective-C:三种文件导入的方式以及atomic和nonatomic的区别
- Objective-C内存管理中的Retain Circle
- Objective-C开发-条件编译
- Objective-C 入门笔记
- NSNumber与NSInteger的区别
- NSArray方法 Objective-c
- 【IOS 开发学习总结-OC-29】★★objective-c之foundation 框架——(集合类)数组
- iOS Object-c 学习笔记
- ObjectAnimator属性动画应用demo
- Objective-C 程序设计 第三章
- 学习prototypejs中的继承实现机制(一): Object.extend()、Class.create()、Class#addMethods()的使用