Description &&debugDescription && runtime(debug模式下调试model)
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
? 其实debugDescription
和description
是一样的效果. 只不过唯一的区别就是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
看如下图, 分别使用了
NSLog
和po
命令的打印
这里写图片描述
结果:
这里写图片描述
这就达到了我们想要的效果, 如果需要打印
model
的属性, 打个断点然后使用po
命令即可
最新文章
- 对TCP/IP网络协议的深入浅出归纳
- ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
- IIS负载均衡
- Opencv不用每次创建项目配置vs2010 vc++目录 库目录等项
- BZOJ1109 : [POI2007]堆积木Klo
- mysql 和字符串截取相关的知识点
- (转)http接口测试——Jmeter接口测试实例讲解
- iOS开发经验总结(转)
- activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
- ExtJS4.2学习(19)在线编辑器Ext.form.HtmlEditor(转)
- 部署服务--NLB
- Spring MVC 统一异常处理
- mysql之7xtrabackup
- DOS/VBS - 用 bat 批处理 实现自动telnet
- WKWebView--JS调用OC的方法
- Linux显示系统日期
- ERP-非财务人员的财务培训教(三)------公司/部门预算编制与评价
- Git实际使用
- CSS-联合选择器
- .NET:bin 与 obj,Debug 与 Release ,区别与选择
热门文章
- WPF设置Window的数据上下文(DataContext)为自身
- Golang之实现一个负载均衡算法(随机,轮询)
- ubuntu系统下安装pyspider:解决pyspider启动时不启动phantomjs问题
- Mysql快速重置root密码
- KbmMW 4.40.00 正式版发布
- 实战:MySQL Sending data导致查询很慢的问题详细分析(转)
- 一文读懂spark yarn集群搭建
- Google’s Project Tango is shutting down because ARCore is already here
- eclipse-->;run as -->; maven test 中文乱码
- VC6.0快捷键一览表