【OBJC类扩展之属性字典】NSObject+Property
2016-06-16 09:24
393 查看
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (Property)
//将对象属性封装到字典,并返回字典
-(NSDictionary *)propertyDictionary;
@end
@implementation NSObject (Property)
-(NSDictionary *)propertyDictionary
{
//创建可变字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned int outCount;
objc_property_t *props = class_copyPropertyList([self class], &outCount);
for(int i=0;i<outCount;i++){
objc_property_t prop = props[i];
NSString *propName = [[NSString alloc]initWithCString:property_getName(prop) encoding:NSUTF8StringEncoding];
id propValue = [self valueForKey:propName];
if(propValue){
[dict setObject:propValue forKey:propName];
}
}
free(props);
return dict;
}
@end
#import <objc/runtime.h>
@interface NSObject (Property)
//将对象属性封装到字典,并返回字典
-(NSDictionary *)propertyDictionary;
@end
@implementation NSObject (Property)
-(NSDictionary *)propertyDictionary
{
//创建可变字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned int outCount;
objc_property_t *props = class_copyPropertyList([self class], &outCount);
for(int i=0;i<outCount;i++){
objc_property_t prop = props[i];
NSString *propName = [[NSString alloc]initWithCString:property_getName(prop) encoding:NSUTF8StringEncoding];
id propValue = [self valueForKey:propName];
if(propValue){
[dict setObject:propValue forKey:propName];
}
}
free(props);
return dict;
}
@end
相关文章推荐
- ios学习路线—Objective-C(生命周期)
- ios学习路线—Objective-C(通知机制)
- ios学习路线—Objective-C(Block)
- Objective-C --- - UICollectionView (梳理总结)
- 在Objective-C中,如何进行内存管理
- Objective-C中遍历字典、数组和集合的几种常见方式
- Objective-C入门知识
- 空对象模式(Null Object Pattern)
- ios学习路线—Objective-C(KVO)
- android群英传objectAnimator的问题
- ios学习路线—Objective-C(KVC)
- ios学习路线—Objective-C(Extension)
- 【ReactNative】undefined is not an object(evaluating 'this.props.navigator')
- ios学习路线—Objective-C(Protocol)
- ios学习路线—Objective-C(Category)
- Scala伴生类Class和伴生对象Object的区别
- Objective-C动画学习之iOS Animation
- ios学习路线—Objective-C(深浅拷贝)
- ios学习路线—Objective-C(检查内存泄露方式(Instruments))
- ios学习路线—Objective-C(检查内存泄露方式(Enable NSZombie Objects))