将jsonModel转化为文件
这个类是我自己写着用的,用于将字典文件直接转换成Model的文件,省去你写无数Model中属性的代码:
TransformDictionary.h 与 TransformDictionary.m
// // TransformDictionary.h // Dic // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> @interface TransformDictionary : NSObject + (void)withDictionary:(NSDictionary *)dictionary fileName:(NSString *)name; @end
// // TransformDictionary.m // Dic // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "TransformDictionary.h" @implementation TransformDictionary + (void)withDictionary:(NSDictionary *)dictionary fileName:(NSString *)name { NSDictionary *dic = dictionary; NSString *shortName = name; NSString *fullHeadName = [name stringByAppendingString:@".h"]; NSString *fullContentName = [name stringByAppendingString:@".m"]; // .h 文件头部信息 __block NSString *headFileString = [NSString stringWithFormat:@"// // %@ // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> @interface %@ : NSObject ", fullHeadName, shortName]; // .h 文件中间信息 [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if ([key isKindOfClass:[NSString class]]) { if ([dic[key] isKindOfClass:[NSString class]]) { headFileString = [headFileString stringByAppendingString:[NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@; ", key]]; } else if ([dic[key] isKindOfClass:[NSNumber class]]) { headFileString = [headFileString stringByAppendingString:[NSString stringWithFormat:@"@property (nonatomic, strong) NSNumber *%@; ", key]]; } else if ([dic[key] isKindOfClass:[NSDictionary class]]) { headFileString = [headFileString stringByAppendingString:[NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@; ", key]]; } else if ([dic[key] isKindOfClass:[NSArray class]]) { headFileString = [headFileString stringByAppendingString:[NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@; ", key]]; } else { NSLog(@"%@ %@",[dic[key] class], key); headFileString = [headFileString stringByAppendingString:[NSString stringWithFormat:@"//@property (nonatomic, strong) %@ *%@; ", [dic[key] class], key]]; } } }]; // - (void)setValue:(id)value forUndefinedKey:(NSString *)key headFileString = [headFileString stringByAppendingString:@" - (void)setValue:(id)value forUndefinedKey:(NSString *)key; "]; // - (instancetype)initWithDictionary:(NSDictionary *)dictionary headFileString = [headFileString stringByAppendingString:@"- (instancetype)initWithDictionary:(NSDictionary *)dictionary; "]; // .h 文件尾部信息 headFileString = [headFileString stringByAppendingString:@" @end "]; // .h 文件路径 NSString *headFileStringPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@", fullHeadName]]; // 写文件 [headFileString writeToFile:headFileStringPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; // ================================================================================= // // .m 文件 NSString *contentFileString = [NSString stringWithFormat:@"// // %@ // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "%@" @implementation %@ ", fullContentName, fullHeadName, shortName]; // .m 文件中间部分 // - (void)setValue:(id)value forUndefinedKey:(NSString *)key contentFileString = [contentFileString stringByAppendingString:@"- (void)setValue:(id)value forUndefinedKey:(NSString *)key { // if([key isEqualToString:@"id"]) { // self.productID = value; // } } "]; // - (void)setValue:(id)value forKey:(NSString *)key contentFileString = [contentFileString stringByAppendingString:@" - (void)setValue:(id)value forKey:(NSString *)key { if ([value isKindOfClass:[NSNull class]]) { return; } [super setValue:value forKey:key]; }"]; // - (instancetype)initWithDictionary:(NSDictionary *)dictionary contentFileString = [contentFileString stringByAppendingString:@" - (instancetype)initWithDictionary:(NSDictionary *)dictionary { self = [super init]; if (self) { if ([dictionary isKindOfClass:[NSDictionary class]]) { [self setValuesForKeysWithDictionary:dictionary]; } } return self; } "]; // .m 文件尾部信息 contentFileString = [contentFileString stringByAppendingString:@" @end"]; // .m 文件路径 NSString *contentFileStringPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@", fullContentName]]; [contentFileString writeToFile:contentFileStringPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; // 提示信息 NSLog(@"生成的文件在以下路径中 %@", [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents"]]); } @end
用工具生成的Model如下: