一行代码生成model.h

作为手艺人还是多学点东西好,不然当你四十岁被公司开了,或者行业不景气了想要跳出这个坑,没有点手艺咋混zzz. web几经在学了貌似web也要.......
不跟你多比比 下面这个分类解决的问题就是 不用手敲model.h 的代码其实也可以不用敲model.m的代码(相信没几个人是手敲的).我只是看到有提出这个需求自己 简单写一个.
链接地址git

上代码
NSObject.h的分类

//  NSObject+CreatProperty.h
//  一行代码生成Model.h
//   Copyright © 2017年 3D. All rights reserved.

#import <Foundation/Foundation.h>
@interface NSObject (CreatProperty)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict andClassStr:(NSString *)classStr andPrdfix:(NSString *)prdfix;
@end

NSObject.m的分类

//  NSObject+CreatProperty.m
//  一行代码生成Model.h
//
//  Created by 3D on 17/2/27.
//  Copyright © 2017年 3D. All rights reserved.

#import "NSObject+CreatProperty.h"

@implementation NSObject (CreatProperty)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict andClassStr:(NSString *)classStr andPrdfix:(NSString *)prdfix
{
  //目前分类中遍历属性类型 还没加完全 而且字典中的NSNumber 都是生的int  先意思一下以后再完善😁
 NSMutableString *strM = [NSMutableString string];
  // 遍历字典
[dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull propertyName, id  _Nonnull value, BOOL * _Nonnull stop) {
    
    //  NSLog(@"%@ %@",propertyName,[value class]);
    NSString *code;
    
    if ([value isKindOfClass:[NSString class]]) {
        code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",propertyName]
        ;
    }else if ([value isKindOfClass:[NSNumber class]]){
        //这里写的不完善 暂时都用int 保存
        code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName]
        ;
    }else if ([value isKindOfClass:[NSArray class]]){
        
        code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName]
        ;
        [self createPropertyCodeWithKey:propertyName andArray:value andPrdfix:prdfix];
    }else if ([value isKindOfClass:[NSDictionary class]]){
        
        code = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",[NSString stringWithFormat:@"%@%@Model",prdfix,propertyName],propertyName]
        ;
        [self createPropertyCodeWithKey:propertyName andDIC:value andPrdfix:prdfix];
    }else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
        code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
        ;
    }
    [strM appendFormat:@"\n%@\n",code];
}];
NSString *top = nil;

top = [NSString stringWithFormat:@"@interface %@ : NSObject",classStr];
NSString *bottom = @"@end";
NSString *allStr= [NSString stringWithFormat:@"\n%@\n%@\n%@\n\n",top,strM,bottom];
const char *cString1 = [allStr cStringUsingEncoding:NSUTF8StringEncoding];
printf("%s",cString1); //这里用c来打印免打印一写不需要的 系统打印
}

+ (void)createPropertyCodeWithKey:(NSString *)key andArray:(NSArray *)arr andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
    NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
    [self createPropertyCodeWithDict:arr[0] andClassStr:classStr andPrdfix:prdfix];
});
}

+ (void)createPropertyCodeWithKey:(NSString *)key andDIC:(NSDictionary *)dic andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
    NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
    [self createPropertyCodeWithDict:dic andClassStr:classStr andPrdfix:prdfix];
});
}
@end

关键点就是下面两个方法

  • (void)createPropertyCodeWithKey:(NSString *)key andArray:(NSArray *)arr andPrdfix:(NSString *)prdfix{
    dispatch_async(dispatch_get_main_queue(), ^{
    NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
    [self createPropertyCodeWithDict:arr[0] andClassStr:classStr andPrdfix:prdfix];
    });
    }

  • (void)createPropertyCodeWithKey:(NSString *)key andDIC:(NSDictionary *)dic andPrdfix:(NSString *)prdfix{
    dispatch_async(dispatch_get_main_queue(), ^{
    NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
    [self createPropertyCodeWithDict:dic andClassStr:classStr andPrdfix:prdfix];
    });
    }

这连个方法是异步主队列 上次我记得已经我已经解释过了 GCD的一些知识
我们要打印model的属性总是希望一层一层的打印.但是在递归打印的时候有可能是这个样的model模型例如

@{@"a":@"属性1"
  @"b":@"属性2"
  @"c":@"属性3:
  @"d":@{@"aa":@"我是对象"}
  @"e":@"属性4"}
  我们想生成
  xxxxxxxxxxx   a
  xxxxxxxxxxx   b
  xxxxxxxxxxx   c
  xxxxxxxxxxx   d
  xxxxxxxxxxx   e
  
  然后在生成
  xxxxxxxxxxx   aa
  你看上面其实是递归操作 遇见value是字典 或者 数组 就有调用

自己那么我们 这个时候第一层还没打印玩 但是又要调用自己打印

下一层我们就可以 把打印下一层的 任务放在异步主队列 (当然打

 印调用这个方法前提是主队列) 异步主队列 第一不会影响 当前线

程的任务(打印下面的属性) 当下面的属性打印完了(主队列空闲了)  

就会执行我们第一次方法主队列的任务,依次进行下去.最后我们只  

要打印台粘贴复制

下面是这个分类的用法

 //  ViewController.m
//  一行代码生成Model.h
//  Created by 3D on 17/2/27.
//  Copyright © 2017年 3D. All rights reserved.

#import "ViewController.h"
#import "LCTestModel.h"
#import "NSObject+CreatProperty.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
//使用前请把xcode8自动打印的log关掉比较 打印台比较清爽
[super viewDidLoad];
 NSDictionary *dic0 = @{@"thumbnail_pic":@"http://ww4.sinaimg.cn/thumbnail/80292f4btw1eqi01myf23j20br08ogm0.jpg",
                      @"name":@"对象中有个数组数组里面是对象"};


NSDictionary *dic =  @{@"attitudes_count":@3,
                       @"created_at":@"刚刚",
                       @"idstr":@"3824316141411024",
                       @"pic_urls":@[dic0],
                       @"user":@{@"name":@"对象面有对象",
                                 @"vip":@1}
                       };
NSDictionary *dic1 =  @{@"attitudes_count":@3,
                       @"created_at":@"刚刚",
                       @"idstr":@"3824316141411024",
                       @"pic_urls":@[dic0],
                       @"user":@{@"name":@"猪猪爱讲冷笑话",
                                 @"vip":@1}
                       };
NSDictionary *dic2 =  @{@"attitudes_count":@3,
                       @"created_at":@"刚刚",
                       @"idstr":@"3824316141411024",
                       @"pic_urls":@[dic0],
                       @"user":@{@"name":@"猪猪爱讲冷笑话",
                                 @"vip":@1}
                       };

NSDictionary *dic3 =  @{@"statuses":@[dic,dic1,dic2],
  @"total_number":@1091};
[LCTestModel createPropertyCodeWithDict:dic3 andClassStr:NSStringFromClass([LCTestModel class]) andPrdfix:@"LC"];//@"LC"就是 model前缀自己定义我叫李长城我就定义LC
}
@end

打印台的效果图就是

WechatIMG52.jpeg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容