工具github地址:https://github.com/linhaosunny/LSXExtension
在开发中经常要将plist文件或者json生成的字典数组生成模型对应的属性头文件,在创建模型属性头文件后,如果实际工程中用到 了字典数组中的大部分属性,如果属性个数比较多的情况,手敲代码去写这些属性是一件非常费时而且容易出错的事情,通过下面的工具可以轻松实现以上代码的头 文件的生成提高开发效率。
// 新增功能 深层次将plist或者json转换成模型头文件
支持多层次的json或者plist文件生成对应的模型头文件支持多层次的json或者plist文件生成对应的模型头文件。
6.强大的plist,json数据转模型功能一句代码实现,比MJExtension更简单。
*对比MJExtension
如MJExtension如果模型文件中还存在模型数据.m文件必须实现如下方法:- (NSDictionary *)objectClassInArray;
如下图LTDealModel中包含了另外一个模型LTBusinessesModel:
.m文件实现如下方法
![](https://img.haomeiwen.com/i3483390/d94a3dd052e8890f.png)
*LSXExtension 不需要实现如下方法.m文件不需要写任何代码
*网络数据转模型实测图如下:
![](https://img.haomeiwen.com/i3483390/bcb13e93e792ed0f.png)
模型中包含模型数组,字典也可以转出来,
*工具自带测试程序效果如下,读取的是plist文件
![](https://img.haomeiwen.com/i3483390/aed77c140c08b55d.png)
5.new(新功能)
增加深层次对plist文件或者json文件转换成模型头文件。
如在plist 文件中属性为字典数组(NSArray <NSDictionary *>*),或者字典(NSDictionary*):
![](https://img.haomeiwen.com/i3483390/804d8a0ce7bab08c.png)
也能转换成模型头文件,会在对应的路径中生成对应的以属性名首字母大写的头文件如下图:
![](https://img.haomeiwen.com/i3483390/f88e1702fe222543.png)
首个头文件中会引用生成的新的模型类型:
![](https://img.haomeiwen.com/i3483390/59570de42d85e75a.png)
字典类型不再显示替换成模型类型NSDictionary ===> xxxModel
![](https://img.haomeiwen.com/i3483390/71bd090db9a13e6d.png)
数组类型会范型引用真实类型 ===>
![](https://img.haomeiwen.com/i3483390/9d8faf9a6a70961f.png)
//====================================================
如将一个status.plist生成对应的StatusModel.h文件
导入LSXExtension.framework后
1.包含如下头文件
#import "LSXExtension.h"
2.使用如下方法生成对应的模型头文件
第一个参数:工程中plist文件名称
第二个参数:需要转换的数据部分字典中的key
第三个参数:模型名称
第四个参数:生成文件路径
[PropertyCodeMake propertyCodeMakeWithPlist:@"status.plist" andDataKey:@"statuses" andModelName:@"StatusModel" andOutFilePath:@"/Users/lishaxin/Desktop"];
3.也可以将一个json转换后的字典数组生成对应的模型头文件
+ (void)propertyCodeMakeWithDictionaryArray:(NSArray *)array andModelName:(NSString *) modelName andOutFilePath:(NSString *) outPath;
4.生成效果如下:
//
// StatusModel.h
//
//
// Created by 李莎鑫 on 2017/03/22.
// Copyright © 2017年 李莎鑫. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface StatusModel : NSObject
// > NSString source
@property (nonatomic, copy) NSString *source;
// > NSNumber reposts_count
@property (nonatomic, assign) NSNumber *reposts_count;
// > NSArray pic_urls
@property (nonatomic, strong) NSArray *pic_urls;
// > NSString created_at
@property (nonatomic, copy) NSString *created_at;
// > NSNumber attitudes_count
@property (nonatomic, assign) NSNumber *attitudes_count;
// > NSString idstr
@property (nonatomic, copy) NSString *idstr;
// > NSString text
@property (nonatomic, copy) NSString *text;
// > NSNumber comments_count
@property (nonatomic, assign) NSNumber *comments_count;
// > NSDictionary user
@property (nonatomic, strong) NSDictionary *user;
// > NSDictionary retweeted_status
@property (nonatomic, strong) NSDictionary *retweeted_status;
@end
网友评论