很喜欢简书的整体设计模式,于是把以前的笔记稍加整理,搬一部分到这里。
通过runtime实现简单的json解析(仿JsonModel模式),只实现了一小小部分功能,并没有做太多的容错处理,主要是为了练习一下runtime机制
部分代码:
#import "CWModel.h"
/*
*"code": "110000",
*"name": {"code": "110101", "name": "东城区"},
*"cell": []
*/
/*
*"code": "110100",
*"name": "市辖区",
*"cell": [{
*"code": "110101",
*"name": "东城区"
*}]
*/
@interface CWAreaModel : CWModel
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *name;
@end
@protocol CWAreaModel @end
@interface CWCityModel : CWModel
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSArray <CWAreaModel>*cell;
@end
@protocol CWCityModel @end
@interface CWChinaModel : CWModel
@property (nonatomic, assign) BOOL ok;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, strong) CWAreaModel *name;
@property (nonatomic, strong) NSArray <CWCityModel>*cell;
@property (nonatomic, strong) NSDictionary *nameDict;
@end
/** 字典转模型 */
+ (instancetype)modelWithDict:(NSDictionary *)dict;
/** 打印 */
+ (NSString *)resolveDict:(NSDictionary *)dict;
Demo中有注释具体实现Demo地址:https://github.com/CyanSmile/RuntimeTest.git