使用runtime给属性批量初始化赋值
2016-03-08 10:43
405 查看
做的时候经常发现一个viewModel类有几十个属性,初始化赋值太麻烦还耗时间,所以就想到runtime,进行批量赋值
要包含头文件:
#import <objc/runtime.h>
具体语句方法
- (instancetype)initWithModel:(id)model {
self = [super
init];
if (!self) {
return
nil;
}
_model = model;
unsigned
int propertyCount = 0;
objc_property_t *properties =
class_copyPropertyList([self
class], &propertyCount);
for (unsigned
int i = 0; i < propertyCount; i ++ ) {
objc_property_t property = properties[i];
const
char *name = property_getName(property);
const
char *attributes = property_getAttributes(property);
NSString *key = [NSString
stringWithUTF8String:name];
NSString *type = [NSString
stringWithUTF8String:attributes];
if ([type
rangeOfString:@"NSString"].location !=
NSNotFound ) {
[self
setValue:@""
forKey:key];
}
}
//RAC(self, catId) =
return
self;
}
这得节约多少时间,时间就是生命,时间就是金钱
要包含头文件:
#import <objc/runtime.h>
具体语句方法
- (instancetype)initWithModel:(id)model {
self = [super
init];
if (!self) {
return
nil;
}
_model = model;
unsigned
int propertyCount = 0;
objc_property_t *properties =
class_copyPropertyList([self
class], &propertyCount);
for (unsigned
int i = 0; i < propertyCount; i ++ ) {
objc_property_t property = properties[i];
const
char *name = property_getName(property);
const
char *attributes = property_getAttributes(property);
NSString *key = [NSString
stringWithUTF8String:name];
NSString *type = [NSString
stringWithUTF8String:attributes];
if ([type
rangeOfString:@"NSString"].location !=
NSNotFound ) {
[self
setValue:@""
forKey:key];
}
}
//RAC(self, catId) =
return
self;
}
这得节约多少时间,时间就是生命,时间就是金钱
相关文章推荐
- Android开发周报:谷歌I/O网站上线、Android架构解析
- Android M 新的运行时权限开发者需要知道的一切
- 怎么用copy关键字
- 设计模式:单例模式
- Python Singleton
- 各类算法技巧核心代码,知识点归纳总结之单调递增子序列
- MultipartFile 转 File
- Actionbarsherlock与Viewpagerindicator详解
- eclipse插件开发调试报错,出现内存溢出的情况
- Vim 配置和使用
- 重温设计模式一:类关系
- ubuntu 下androidsdk 更新
- ubuntu 除home目录,其他目录修改时权限的修改
- 公钥和私钥解释
- android apk 源码 的 破译(反编译)
- mybatis beanupdate报错
- archLinux ibus 中文 双拼音的安装
- archLinux VirtualBox增强设置
- C# -- 使用委托
- IE7 自动为文件路径添加域名