码迷,mamicode.com
首页 > 其他好文 > 详细

【OBJC类扩展之属性字典】NSObject+Property

时间:2016-06-16 10:32:33      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

#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

【OBJC类扩展之属性字典】NSObject+Property

标签:

原文地址:http://blog.csdn.net/qq_27633421/article/details/51689433

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!