作為手藝人還是多學(xué)點(diǎn)東西好,不然當(dāng)你四十歲被公司開了,或者行業(yè)不景氣了想要跳出這個(gè)坑,沒有點(diǎn)手藝咋混zzz. web幾經(jīng)在學(xué)了貌似web也要.......
不跟你多比比 下面這個(gè)分類解決的問題就是 不用手敲model.h 的代碼其實(shí)也可以不用敲model.m的代碼(相信沒幾個(gè)人是手敲的).我只是看到有提出這個(gè)需求自己 簡(jiǎn)單寫一個(gè).
鏈接地址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]]){
//這里寫的不完善 暫時(shí)都用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來打印免打印一寫不需要的 系統(tǒng)打印
}
+ (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
關(guān)鍵點(diǎn)就是下面兩個(gè)方法
(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];
});
}
這連個(gè)方法是異步主隊(duì)列 上次我記得已經(jīng)我已經(jīng)解釋過了 GCD的一些知識(shí)
我們要打印model的屬性總是希望一層一層的打印.但是在遞歸打印的時(shí)候有可能是這個(gè)樣的model模型例如
@{@"a":@"屬性1"
@"b":@"屬性2"
@"c":@"屬性3:
@"d":@{@"aa":@"我是對(duì)象"}
@"e":@"屬性4"}
我們想生成
xxxxxxxxxxx a
xxxxxxxxxxx b
xxxxxxxxxxx c
xxxxxxxxxxx d
xxxxxxxxxxx e
然后在生成
xxxxxxxxxxx aa
你看上面其實(shí)是遞歸操作 遇見value是字典 或者 數(shù)組 就有調(diào)用
自己那么我們 這個(gè)時(shí)候第一層還沒打印玩 但是又要調(diào)用自己打印
下一層我們就可以 把打印下一層的 任務(wù)放在異步主隊(duì)列 (當(dāng)然打
印調(diào)用這個(gè)方法前提是主隊(duì)列) 異步主隊(duì)列 第一不會(huì)影響 當(dāng)前線
程的任務(wù)(打印下面的屬性) 當(dāng)下面的屬性打印完了(主隊(duì)列空閑了)
就會(huì)執(zhí)行我們第一次方法主隊(duì)列的任務(wù),依次進(jìn)行下去.最后我們只
要打印臺(tái)粘貼復(fù)制
下面是這個(gè)分類的用法
// 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 {
//使用前請(qǐng)把xcode8自動(dòng)打印的log關(guān)掉比較 打印臺(tái)比較清爽
[super viewDidLoad];
NSDictionary *dic0 = @{@"thumbnail_pic":@"http://ww4.sinaimg.cn/thumbnail/80292f4btw1eqi01myf23j20br08ogm0.jpg",
@"name":@"對(duì)象中有個(gè)數(shù)組數(shù)組里面是對(duì)象"};
NSDictionary *dic = @{@"attitudes_count":@3,
@"created_at":@"剛剛",
@"idstr":@"3824316141411024",
@"pic_urls":@[dic0],
@"user":@{@"name":@"對(duì)象面有對(duì)象",
@"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前綴自己定義我叫李長(zhǎng)城我就定義LC
}
@end
打印臺(tái)的效果圖就是