#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
最新推荐文章于 2022-10-21 01:29:28 发布
本文详细探讨了Objective-C与Swift两种语言在iOS开发领域的应用与区别,包括其优势、使用场景以及如何在实际项目中进行选择。
606

被折叠的 条评论
为什么被折叠?



