ios自动生成对象类,提高开发速率
#import "autoGenerationFileUtility.h" @implementation autoGenerationFileUtility - (void)createFile:(NSString *)filename DataSource:(NSMutableArray *)arr
{
_ClassName = [filename retain];
NSMutableArray *tempArr = [arr retain]; // 创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager]; // // 获取docoument路经,括号中属性为当前应用程序独享
// NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//
// // 去处需要的路径
// NSString *documentDirectory = [directoryPaths objectAtIndex:0]; NSString* documentDirectory = @"具体路径,文件生成路径"; // 创建.h文件路经
NSString *pathH = [self createFilePath:[NSString stringWithFormat:@"%@.h",_ClassName] PathDocument:documentDirectory FileManager:fileManager];
// 创建.m文件路经
NSString *pathM = [self createFilePath:[NSString stringWithFormat:@"%@.m",_ClassName] PathDocument:documentDirectory FileManager:fileManager]; // NSString * pathH_Content = [[NSString alloc] initWithContentsOfFile:pathH];
NSData *pathH_Content = [[[NSData alloc] initWithContentsOfFile:pathH] autorelease];
if (pathH_Content.length==) {
// 写入 .h 文件数据
[self H_populateTheDataSource:tempArr FilePathH:pathH FilePathM:pathM];
} [tempArr release];
} // 创建文件、如果文件名存在,则先删除已存在的文件,再创建新文件
- (NSString *)createFilePath:(NSString *)filename PathDocument:(NSString *)documentDirectory FileManager:(NSFileManager *)fileManager
{
NSString *filePath = [documentDirectory stringByAppendingPathComponent:filename]; // [fileManager removeItemAtPath:filePath error:nil]; if (![fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
} return filePath;
} - (void)H_populateTheDataSource:(NSMutableArray *)data FilePathH:(NSString *)path_h FilePathM:(NSString *)path_m
{
if (data.count <= ) {
return;
}
// 创建数据缓冲
NSMutableData *writerH = [[NSMutableData alloc]init];
NSMutableData *writerM = [[NSMutableData alloc]init]; NSMutableArray *newArray = [[NSMutableArray alloc]init]; //存放每次field的数据 NSDictionary *newDic = nil;
for (int i = ; i < data.count; i++) {
NSDictionary *dic = [data objectAtIndex:i]; if (i == ) {
newDic = dic;
[writerH appendData:[[self H_addHeaderFileString] dataUsingEncoding:NSUTF8StringEncoding]];
[writerH appendData:[[self H_addClassNameString:[dic objectForKey:@"classname"]] dataUsingEncoding:NSUTF8StringEncoding]];
[writerH appendData:[[self H_addLeftKuoHao] dataUsingEncoding:NSUTF8StringEncoding]]; NSString *tempStr = [self H_addField:[dic objectForKey:@"fieldname"] ClassType:[dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addHeaderFileString:_ClassName] dataUsingEncoding:NSUTF8StringEncoding]];
[writerM appendData:[[self M_addClassNameString:[dic objectForKey:@"classname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addSynthesizeName:[dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray addObject:dic];
}
else{
if (i == data.count - ) { NSString *tempStr = [self H_addField:[dic objectForKey:@"fieldname"] ClassType:[dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]];
[writerH appendData:[[self H_addRightKuoHao] dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addSynthesizeName:[dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray addObject:dic]; for (NSDictionary *_dic in newArray) { NSString *tempStr = [self H_addProperty:[_dic objectForKey:@"fieldname"] ClassType:[_dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]]; Class retClass = NSClassFromString([_dic objectForKey:@"type"]);
if ([[NSString stringWithFormat:@"%@",[retClass class]] isEqualToString:@"NSMutableArray"]){ [writerM appendData:[[self M_addATArray:[_dic objectForKey:@"fieldname"] ClassName:
[NSString stringWithFormat:@"Buy_%@",[_dic objectForKey:@"fieldname"]]] dataUsingEncoding:NSUTF8StringEncoding]];
}
} [writerH appendData:[[self addEnd] dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addDealloc1] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSDictionary *_dic in newArray) {
Class retClass = NSClassFromString([_dic objectForKey:@"type"]);
if (retClass) {
[writerM appendData:[[self M_addRelProperty:[_dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
} [writerM appendData:[[self M_addDealloc2] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray removeAllObjects];
}
else{ if ([[dic objectForKey:@"index"] isEqualToString:[newDic objectForKey:@"index"]] &&
[[dic objectForKey:@"classname"] isEqualToString:[newDic objectForKey:@"classname"]]) {
NSString *tempStr = [self H_addField:[dic objectForKey:@"fieldname"] ClassType:[dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addSynthesizeName:[dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray addObject:dic];
}
else{
newDic = dic; [writerH appendData:[[self H_addRightKuoHao] dataUsingEncoding:NSUTF8StringEncoding]]; for (NSDictionary *_dic in newArray) { NSString *tempStr = [self H_addProperty:[_dic objectForKey:@"fieldname"] ClassType:[_dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]]; Class retClass = NSClassFromString([_dic objectForKey:@"type"]);
if ([[NSString stringWithFormat:@"%@",[retClass class]] isEqualToString:@"NSMutableArray"]){
[writerM appendData:[[self M_addATArray:[_dic objectForKey:@"fieldname"] ClassName:[NSString stringWithFormat:@"Buy_%@",[_dic objectForKey:@"fieldname"]]] dataUsingEncoding:NSUTF8StringEncoding]];
}
} [writerH appendData:[[self addEnd] dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addDealloc1] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSDictionary *_dic in newArray) {
Class retClass = NSClassFromString([_dic objectForKey:@"type"]);
if (retClass) {
[writerM appendData:[[self M_addRelProperty:[_dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[writerM appendData:[[self M_addDealloc2] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray removeAllObjects]; [writerH appendData:[[self H_addClassNameString:[newDic objectForKey:@"classname"]] dataUsingEncoding:NSUTF8StringEncoding]];
[writerH appendData:[[self H_addLeftKuoHao] dataUsingEncoding:NSUTF8StringEncoding]]; NSString *tempStr = [self H_addField:[dic objectForKey:@"fieldname"] ClassType:[dic objectForKey:@"type"]];
[writerH appendData:[tempStr dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addClassNameString:[newDic objectForKey:@"classname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [writerM appendData:[[self M_addSynthesizeName:[dic objectForKey:@"fieldname"]] dataUsingEncoding:NSUTF8StringEncoding]]; [newArray addObject:dic]; }
}
}
} [writerH writeToFile:path_h atomically:YES];
[writerM writeToFile:path_m atomically:YES];
[writerH release];
[writerM release];
[newArray release]; } #pragma mark -
#pragma mark - 写入 .h 文件的相关函数 // 返回头文件
- (NSString *)H_addHeaderFileString
{
return @"#import <Foundation/Foundation.h> \n \n \n";
} // 返回类名
- (NSString *)H_addClassNameString:(NSString *)className;
{
return [NSString stringWithFormat:@"@interface %@ : NSObject \n",className];
} // 返回左括号
- (NSString *)H_addLeftKuoHao
{
return @"{ \n";
} // 返回右括号
- (NSString *)H_addRightKuoHao
{
return @"} \n \n";
} // 返回字段
- (NSString *)H_addField:(NSString *)field ClassType:(NSString *)type
{
Class retClass = NSClassFromString(type);
if (retClass)
return [NSString stringWithFormat:@" %@ *%@; \n",type,field];
else
return [NSString stringWithFormat:@" %@ %@; \n",type,field];
} // 返回属性
- (NSString *)H_addProperty:(NSString *)property ClassType:(NSString *)type
{
Class retClass = NSClassFromString(type);
if (retClass) {
return [NSString stringWithFormat:@"@property (nonatomic,retain) %@ *%@; \n",type,property];
}
else
return [NSString stringWithFormat:@"@property %@ %@; \n",type,property];
} #pragma mark -
#pragma mark - 写入 .m 文件的相关函数 // 返回头文件
- (NSString *)M_addHeaderFileString:(NSString *)classname
{
return [NSString stringWithFormat:@"#import \"%@.h\"\n #import \"NSDictionary+JSON.h\" \n \n",classname];
} // 返回类名
- (NSString *)M_addClassNameString:(NSString *)className;
{
return [NSString stringWithFormat:@"@implementation %@ \n",className];
} // 返回@synthesize
- (NSString *)M_addSynthesizeName:(NSString *)property
{
return [NSString stringWithFormat:@"@synthesize %@; \n",property];
} - (NSString *)M_addATArray:(NSString *)property ClassName:(NSString *)classname
{
NSString *retString = [NSString stringWithFormat:@"- (NSMutableArray *)%@ \n{\n",property];
retString = [NSString stringWithFormat:@"%@ for (int i = 0; i < %@.count; i++) {\n",retString,property];
retString = [NSString stringWithFormat:@"%@ if([[%@ objectAtIndex:i] class] != [%@ class]) {\n",retString,property,classname];
retString = [NSString stringWithFormat:@"%@ NSDictionary * _dic = (NSDictionary *)[%@ objectAtIndex:i];\n",retString,property];
retString = [NSString stringWithFormat:@"%@ [%@ replaceObjectAtIndex:i withObject:[_dic dictionaryTo:NSClassFromString(@\"%@\")]];\n } \n",retString,property,classname];
retString = [NSString stringWithFormat:@"%@ }\n",retString];
retString = [NSString stringWithFormat:@"%@ return %@;\n}\n \n",retString,property]; return retString;
} // 返回释放属性
- (NSString *)M_addRelProperty:(NSString *)property
{
return [NSString stringWithFormat:@" [%@ release];\n",property];
} - (NSString *)M_addDealloc1
{
return @"\n \n-(void)dealloc \n{";
} - (NSString *)M_addDealloc2
{
return @" [super dealloc]; \n} \n@end \n\n";
} // 返回结束符
- (NSString *)addEnd
{
return @"\n @end \n \n";
} - (void)dealloc
{
[_ClassName release]; [super dealloc];
} @end
ios自动生成对象类,提高开发速率的更多相关文章
- 【原创】有关Silverlight中自动生成的类中 没有WCF层edmx模型新加入的对象 原因分析。
前端页面层: 编译老是不通过,报如下如所示错误: -- 然后下意识的查了下 生成的cs文件,没有搜到根据edmx 生成的 对应的类. 结果整理: 1.尽管在 edmx 模 ...
- Springboot mybatis generate 自动生成实体类和Mapper
https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...
- Mybatis自动生成实体类
Maven自动生成实体类需要的jar包 一.pom.xml中 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
- .NET SourceGenerators 根据 HTTPAPI 接口自动生成实现类
目录 摘要 元数据分析 使用 Source generators 实现 使用 Source generators 实现程序集分析 使用方法 SourceCode && Nuget pa ...
- 使用T4为数据库自动生成实体类
T4 (Text Template Transformation Toolkit) 是一个基于模板的代码生成器.使用T4你可以通过写一些ASP.NET-like模板,来生成C#, T-SQL, XML ...
- ANTLR和StringTemplate实例:自动生成单元测试类
ANTLR和StringTemplate实例:自动生成单元测试类 1. ANTLR语法 要想自动生成单元测试,首先第一步就是分析被测试类.这里以Java代码为例,用ANTLR对Java代码进行分析.要 ...
- Mybatis自动生成实体类、dao接口和mapping映射文件
由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类.dao接口以及它的映射文件,然后直接拷贝到 ...
- mybatis怎样自动生成java类,配置文件?
其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了. 所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包.当然这些配置也很简单. 为了有个初步的认识 ...
- .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类
引言 由公司需要使用dapper 同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions 并配套 生成实体类小工具的方 ...
随机推荐
- HDU 3480 DP 斜率优化 Division
把n个数分成m段,每段的值为(MAX - MIN)2,求所能划分得到的最小值. 依然是先从小到大排个序,定义状态d(j, i)表示把前i个数划分成j段,所得到的最小值,则有状态转移方程: d(j, i ...
- Appium解锁九宫格(TouchAction)
TouchAction 1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py class TouchActio ...
- 面试准备——Zookeeper
转自https://www.cnblogs.com/shan1393/p/9479109.html 1. Zookeeper是什么框架 分布式的.开源的分布式应用程序协调服务,原本是Hadoop.HB ...
- LiveScript 字面量
The LiveScript Book 字面值 Numbers .4是被视为无效的,你必须使用前导0.例如:0.4. 1.20162.12.343.0.4 数字之间的下划线和数值后追加的字母将 ...
- NYIS OJ 42 一笔画问题
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- (转)iOS字体
一.iOS原生字体展示 在 label中选择字体的font,并把font由system改成custom后,就能在family中看到72种特殊字体.这些里面就有很炫的字体,但 是全部是只针对英文数字,对 ...
- BZOJ 3450 Tyvj1952 Easy ——期望DP
维护$x$和$x^2$的期望递推即可 #include <map> #include <ctime> #include <cmath> #include <q ...
- BZOJ 1260: [CQOI2007]涂色paint【区间DP】
Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...
- BZOJ1926 [Sdoi2010]粟粟的书架 【主席树 + 二分 + 前缀和】
题目 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架,书架的每一个位置都 ...
- BZOJ3996 [TJOI2015]线性代数 【最小割】
题目 给出一个NN的矩阵B和一个1N的矩阵C.求出一个1*N的01矩阵A.使得 D=(AB-C)A^T最大.其中A^T为A的转置.输出D 输入格式 第一行输入一个整数N,接下来N行输入B矩阵,第i行第 ...