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==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> \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
相關文章
相關標籤/搜索