#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==0) { // 写入 .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 <= 0) { return; } // 创建数据缓冲 NSMutableData *writerH = [[NSMutableData alloc]init]; NSMutableData *writerM = [[NSMutableData alloc]init]; NSMutableArray *newArray = [[NSMutableArray alloc]init]; //存放每次field的数据 NSDictionary *newDic = nil; for (int i = 0; i < data.count; i++) { NSDictionary *dic = [data objectAtIndex:i]; if (i == 0) { 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 - 1) { 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> "; } // 返回类名 - (NSString *)H_addClassNameString:(NSString *)className; { return [NSString stringWithFormat:@"@interface %@ : NSObject ",className]; } // 返回左括号 - (NSString *)H_addLeftKuoHao { return @"{ "; } // 返回右括号 - (NSString *)H_addRightKuoHao { return @"} "; } // 返回字段 - (NSString *)H_addField:(NSString *)field ClassType:(NSString *)type { Class retClass = NSClassFromString(type); if (retClass) return [NSString stringWithFormat:@" %@ *%@; ",type,field]; else return [NSString stringWithFormat:@" %@ %@; ",type,field]; } // 返回属性 - (NSString *)H_addProperty:(NSString *)property ClassType:(NSString *)type { Class retClass = NSClassFromString(type); if (retClass) { return [NSString stringWithFormat:@"@property (nonatomic,retain) %@ *%@; ",type,property]; } else return [NSString stringWithFormat:@"@property %@ %@; ",type,property]; } #pragma mark - #pragma mark - 写入 .m 文件的相关函数 // 返回头文件 - (NSString *)M_addHeaderFileString:(NSString *)classname { return [NSString stringWithFormat:@"#import "%@.h" #import "NSDictionary+JSON.h" ",classname]; } // 返回类名 - (NSString *)M_addClassNameString:(NSString *)className; { return [NSString stringWithFormat:@"@implementation %@ ",className]; } // 返回@synthesize - (NSString *)M_addSynthesizeName:(NSString *)property { return [NSString stringWithFormat:@"@synthesize %@; ",property]; } - (NSString *)M_addATArray:(NSString *)property ClassName:(NSString *)classname { NSString *retString = [NSString stringWithFormat:@"- (NSMutableArray *)%@ { ",property]; retString = [NSString stringWithFormat:@"%@ for (int i = 0; i < %@.count; i++) { ",retString,property]; retString = [NSString stringWithFormat:@"%@ if([[%@ objectAtIndex:i] class] != [%@ class]) { ",retString,property,classname]; retString = [NSString stringWithFormat:@"%@ NSDictionary * _dic = (NSDictionary *)[%@ objectAtIndex:i]; ",retString,property]; retString = [NSString stringWithFormat:@"%@ [%@ replaceObjectAtIndex:i withObject:[_dic dictionaryTo:NSClassFromString(@"%@")]]; } ",retString,property,classname]; retString = [NSString stringWithFormat:@"%@ } ",retString]; retString = [NSString stringWithFormat:@"%@ return %@; } ",retString,property]; return retString; } // 返回释放属性 - (NSString *)M_addRelProperty:(NSString *)property { return [NSString stringWithFormat:@" [%@ release]; ",property]; } - (NSString *)M_addDealloc1 { return @" -(void)dealloc {"; } - (NSString *)M_addDealloc2 { return @" [super dealloc]; } @end "; } // 返回结束符 - (NSString *)addEnd { return @" @end "; } - (void)dealloc { [_ClassName release]; [super dealloc]; } @end