美文网首页程序员iOS 干货整理
iOS-懒人教程-自动生成property到.h文件-OC

iOS-懒人教程-自动生成property到.h文件-OC

作者: 清無 | 来源:发表于2016-07-30 14:05 被阅读591次

    效果图

    final cut.png
    实现原理
    1. 配置Info.plist文件,加入自定义的字段:
      ProjectDir : $(SRCROOT)/$(PROJECT_NAME)
    2. 遍历dictionary中的key和value,拼接字符串:@property(nonatomic,cop)NSString *key;
    3. 分类整理-纯为了简洁
    4. 保存到指定的.h文件中
    5. 用NSAssert进行了出错提示,比如没有配置ProjectDir、.h文件找不到、.h文件读取失败
    配置Info.plist
    Info.plist.png
    实现代码-核心

    PropertiesGenerator.m

    // 分类
        NSMutableArray *propertiesArray = [NSMutableArray array];
        NSMutableArray *NSStringArray = [NSMutableArray array];
        NSMutableArray *NSDictinoaryArray = [NSMutableArray array];
        NSMutableArray *NSArrayArray = [NSMutableArray array];
        NSMutableArray *BOOLArray = [NSMutableArray array];
        NSMutableArray *floatArray = [NSMutableArray array];
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSString *str;
            if ([self class:obj isKindOfClass:@"__NSCFString"] || [self class:obj isKindOfClass:@"NSTaggedPointerString"] || [self class:obj isKindOfClass:@"__NSCFConstantString"]) {
                str = [NSString stringWithFormat:@"@property(nonatomic,copy)NSString *%@;",key];
                [NSStringArray addObject:str];
            }
            else if ([self class:obj isKindOfClass:@"__NSCFNumber"]) {
                str = [NSString stringWithFormat:@"@property(nonatomic,assign)float %@;",key];
                [floatArray addObject:str];
            }
            else if ([self class:obj isKindOfClass:@"__NSCFArray"] || [self class:obj isKindOfClass:@"__NSArrayI"]) {
                str = [NSString stringWithFormat:@"@property(nonatomic,copy)NSArray *%@;",key];
                [NSArrayArray addObject:str];
            }
            else if ([self class:obj isKindOfClass:@"__NSCFDictionary"] || [self class:obj isKindOfClass:@"__NSDictionaryI"]) {
                str = [NSString stringWithFormat:@"@property(nonatomic,copy)NSDictionary *%@;",key];
                [NSDictinoaryArray addObject:str];
            }
            else if ([self class:obj isKindOfClass:@"__NSCFBoolean"]) {
                str = [NSString stringWithFormat:@"@property(nonatomic,assign)BOOL %@;",key];
                [BOOLArray addObject:str];
            }
        }];
    
        [propertiesArray addObjectsFromArray: NSStringArray];
        [propertiesArray addObjectsFromArray: NSDictinoaryArray];
        [propertiesArray addObjectsFromArray: NSArrayArray];
        [propertiesArray addObjectsFromArray: BOOLArray];
        [propertiesArray addObjectsFromArray: floatArray];
        propertiesString = [propertiesArray componentsJoinedByString:@"\n"];
    
    // 然后写入到.h文件中
    
    用法
    #import "PropertiesGenerator.h"
         NSDictionary *dict = @{
                               @"name": @"zhuxuhong",
                               @"age": [NSNumber numberWithFloat:22],
                               @"nickname": @"flytoo",
                               @"friends": @[@"1",@"2",@"3"],
                               @"experience": @{@"2016": @"BISTU"},
                               @"password": @"1234",
                               @"education": @{@"2016": @"BISTU"},
                               @"graduated": [NSNumber numberWithBool:true]
                               };
        
        [[PropertiesGenerator generator] autoGeneratePropertiesInModelClass:@"Me" Dictionary:dict]; //判断是否写入成功
    
    github源文件

    https://github.com/BackWorld/PropertiesGenerator

    相关文章

      网友评论

        本文标题:iOS-懒人教程-自动生成property到.h文件-OC

        本文链接:https://www.haomeiwen.com/subject/rfhtsttx.html