偷懒啦!字典自动生成属性列表

在网络请求或加载plist文件时,常会获得一个字典。我们通常会将字典转为模型。就避免不了在类的.h文件声明属性,这是一个重复的工作,我们如何偷懒呢?

假设我们拥有一个plist文件,我们需要对其模型化。首先我们加载这个文件
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 获取文件全路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"status.plist" ofType:nil];
    
    // 文件全路径
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    // 设计模型,创建属性代码 => dict
    [dict createPropertyCode];
}
其次我们声明一个NSDictionary的分类,实现方法createPropertyCode

@interface NSDictionary (Property)

- (void)createPropertyCode;

@end


@implementation NSDictionary (Property)
// isKindOfClass:判断是否是当前类或者子类
// 生成属性代码 => 根据字典中所有key
- (void)createPropertyCode
{
    NSMutableString *codes = [NSMutableString string];
    // 遍历字典
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        
        NSString *code;
        if ([value isKindOfClass:[NSString class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",key];
        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
        } else if ([value isKindOfClass:[NSNumber class]]) {
             code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",key];
        } else if ([value isKindOfClass:[NSArray class]]) {
             code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];
        } else if ([value isKindOfClass:[NSDictionary class]]) {
             code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];
        }
        // @property (nonatomic, strong) NSString *source;
        [codes appendFormat:@"\n%@\n",code];
    }];
  
    NSLog(@"%@",codes);
}

@end
viewDidLoad执行到[dict createPropertyCode];时,就会打印出所有的属性列表:
2016-09-12 11:23:18.646 05-Runtime(****字典转模型****KVC****实现****)[11614:103900] **
@property (nonatomic, strong) NSString *source;

@property (nonatomic, assign) NSInteger reposts_count;

@property (nonatomic, strong) NSArray *pic_urls;

@property (nonatomic, strong) NSString *created_at;

@property (nonatomic, assign) BOOL isA;

@property (nonatomic, assign) NSInteger attitudes_count;

@property (nonatomic, strong) NSString *idstr;

@property (nonatomic, strong) NSString *text;

@property (nonatomic, assign) NSInteger comments_count;

@property (nonatomic, strong) NSDictionary *user;

这是在看小马哥视频时,学习到的方法,记录下来。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、深复制和浅复制的区别? 1、浅复制:只是复制了指向对象的指针,即两个指针指向同一块内存单元!而不复制指向对象的...
    iOS_Alex阅读 1,423评论 1 27
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,203评论 30 471
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 1,393评论 0 3
  • 现在都过了作业雨时间了,可是睡不着,因为再不交作业真的就是出局了,可是唯一让我舍不得的是这群里这么多积极努力上进,...
    田野_4750阅读 302评论 7 5
  • 这是专栏“短视频创作方法论”第一篇,想系统讲述好莱坞电影中特效的运用的“道”,以前的研究,主要在“器”的层面研究特...
    海哥0001阅读 564评论 6 2