description

在开发过程中, 往往会有很多的model来装载属性. 而在开发期间经常会进行调试查看model里的属性值是否正确. 那么问题来了, 在objective-c里使用NSLog("%@",model)这行代码打印出来的却是model的地址. 不是我们所想要的结果~! 看图:

那么问题又来了?有没有办法解决这个问题尼,答案那就是有~!只需要重写- (NSString *)description方法即可。如下代码:

.h文件

#import <Foundation/Foundation.h>

@interface TestModel : NSObject
@property (copy,nonatomic) NSString *text;
@property (assign,nonatomic) NSInteger index;
@end

.m文件

#import "TestModel.h"

@implementation TestModel
- (NSString *)description {
return [NSString stringWithFormat:@"text:%@--index:%zi",self.text,self.index];
}
@end

然后这时候在使用NSLog("%@",model)这行代码就能打印我们想要的结果了。 看如下图:

那么问题继续来了...
如果model里有N多个属性尼, 可能10个, 可能20个... 难道要在description方法里一个一个写属性并拼接返回? 你不嫌麻烦, 我光看着都蛋疼了... 所以我们可以采用runtime技术来动态获取属性并返回. 如下修改后的.m文件代码:

修改后的.m文件

#import "TestModel.h"
#import <objc/runtime.h>//导入runtime头文件 @implementation TestModel
- (NSString *)description {
//初始化一个字典
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //得到当前class的所有属性
uint count;
objc_property_t *properties = class_copyPropertyList([self class], &count); //循环并用KVC得到每个属性的值
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *name = @(property_getName(property));
id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
[dictionary setObject:value forKey:name];//装载到字典里
} //释放
free(properties); //return
return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}
@end

然后在打印model, 如下图:

这里写图片描述

debugDescription

现在问题继续来了..
在项目中NSLog语句往往也很多. 如果重写description方法. 在控制台则会打印出很多属性. 看着就不舒服~~而且还有一个问题就是, 有时候我们其实并不需要打印model的属性.. 那这样重写description方法反而适得其反了! 所有, 现在有一个解决方案就是重写debugDescription方法


什么是debugDescription? 其实debugDescriptiondescription是一样的效果. 只不过唯一的区别就是debugDescription是在Xcode控制台里使用po命令的时候调用的~!


debugDescription的实现其实也就是调用了description方法而已

so, 在开发过程中并且model调试的时候, 笔者推荐重写debugDescription方法而不是重写description方法. 当需要打印model的属性的时候, 在控制台里使用po命令即可. 如下在此修改后的.m文件

#import "TestModel.h"
#import <objc/runtime.h>//导入runtime头文件 @implementation TestModel // 重写debugDescription, 而不是description
- (NSString *)debugDescription {
//声明一个字典
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //得到当前class的所有属性
uint count;
objc_property_t *properties = class_copyPropertyList([self class], &count); //循环并用KVC得到每个属性的值
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *name = @(property_getName(property));
id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
[dictionary setObject:value forKey:name];//装载到字典里
} //释放
free(properties); //return
return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}
@end

看如下图, 分别使用了NSLogpo命令的打印


这里写图片描述

结果:

这里写图片描述

这就达到了我们想要的效果, 如果需要打印model的属性, 打个断点然后使用po命令即可

最新文章

  1. 对TCP/IP网络协议的深入浅出归纳
  2. ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
  3. IIS负载均衡
  4. Opencv不用每次创建项目配置vs2010 vc++目录 库目录等项
  5. BZOJ1109 : [POI2007]堆积木Klo
  6. mysql 和字符串截取相关的知识点
  7. (转)http接口测试——Jmeter接口测试实例讲解
  8. iOS开发经验总结(转)
  9. activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
  10. ExtJS4.2学习(19)在线编辑器Ext.form.HtmlEditor(转)
  11. 部署服务--NLB
  12. Spring MVC 统一异常处理
  13. mysql之7xtrabackup
  14. DOS/VBS - 用 bat 批处理 实现自动telnet
  15. WKWebView--JS调用OC的方法
  16. Linux显示系统日期
  17. ERP-非财务人员的财务培训教(三)------公司/部门预算编制与评价
  18. Git实际使用
  19. CSS-联合选择器
  20. .NET:bin 与 obj,Debug 与 Release ,区别与选择

热门文章

  1. WPF设置Window的数据上下文(DataContext)为自身
  2. Golang之实现一个负载均衡算法(随机,轮询)
  3. ubuntu系统下安装pyspider:解决pyspider启动时不启动phantomjs问题
  4. Mysql快速重置root密码
  5. KbmMW 4.40.00 正式版发布
  6. 实战:MySQL Sending data导致查询很慢的问题详细分析(转)
  7. 一文读懂spark yarn集群搭建
  8. Google’s Project Tango is shutting down because ARCore is already here
  9. eclipse--&gt;run as --&gt; maven test 中文乱码
  10. VC6.0快捷键一览表