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 并配套 生成实体类小工具的方 ...
随机推荐
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...
- 刷题总结——学姐的逛街计划(vijos1891费用流)
题目: doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪学姐去逛街呢. 后来, ...
- 浅谈Oracle数据库分区表
Oracle数据库分区是作为Oracle数据库性能优化的一种重要的手段和方法,之前,只听过分区的大名,却总未用过,最近简单学习了一下,总结如下,不对之处,还希望朋友们多多指点,交流! 1.表空间及分区 ...
- JMS API学习总结(一)
三.JMS API简析 顶级接口 P2P Pub/sub 备注 ConnectionFactory QueueConnectionFactory TopicConnectionFactory 基于工厂 ...
- Hadoop 3.1.0 在 Ubuntu 16.04 上的安装过程
安装过程主要参考官方文档: http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/SingleCluster. ...
- 51nod1026 矩阵中不重复的元素 V2
$n \leq 500000,m \leq 500000$的矩阵,第一行第一列是$a^b,2 \leq a,b \leq 500000$,如果一个数是$i^j$那他右边是$i^{j+1}$,下面是${ ...
- MongoDB数据关系的表达
虽说MongoDB是非关系型数据库,但由于大部分情况下数据之间是存在关系的,所以MongoDB也需要一些方式来表达数据之间的关系.MongoDB表达数据关系的方式有两种:文档嵌套和数据库引用. 一.文 ...
- Day 10 Linux nfs && crond(摘)
(摘) 介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系 ...
- Android开发之(1)AnimationListener
1,就像Button控件有监听器一样,动画效果也有监听器,只需要实现AnimationListener就可以实现对动画效果的监听,只需要实现AnimationListener就可以实现对动画效果的监听 ...
- T1164 统计数字 codevs
http://codevs.cn/problem/1164/ 题目描述 Description [问题描述]某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不 ...