zoukankan      html  css  js  c++  java
  • 使用代码自动创建模型属性

    每次Model中有很多的属性,写起来很费劲!这篇文章将实现一句话创建Model中的所有属性代码,输出到控制台!!!

    NSObject+Property.h

    #import <Foundation/Foundation.h>
    
    @interface NSObject (Property)
    
    + (void)createPropertyCodeWithDictionary:(NSDictionary *)dictionary;
    
    @end

    NSObject+Property.m

    #import "NSObject+Property.h"
    
    @implementation NSObject (Property)
    
    + (void)createPropertyCodeWithDictionary:(NSDictionary *)dictionary
    {
        NSMutableString *strM = [NSMutableString string];
    
        // 遍历字典
        [dictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull propertyName, id  _Nonnull value, BOOL * _Nonnull stop) {
            // NSLog(@"%@ %@",propertyName,[value class]);
            NSString *code;
            if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
                code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName]
                ;
            }else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
                code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName]
                ;
            }else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
                code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName]
                ;
            }else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
                code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",propertyName]
                ;
            }else {
                //未完待续......
            }
            [strM appendFormat:@"
    %@
    ",code];
        }];
        
         NSLog(@"%@",strM);
    }
    
    @end

    更多内容--> 博客导航 每周一篇哟!!!

    有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!

  • 相关阅读:
    vue2 生命周期
    javascript http库axios
    vue2自定义事件之$emit
    php配置rewrite模块
    php 正则匹配中文(转)
    php常用自定义函数
    Mysql----MySQL的mysql_insert_id和LAST_INSERT_ID(转)
    Mysql----mysql启动服务时提示"服务名无效"
    div+css布局
    php常见问题以及解决方法
  • 原文地址:https://www.cnblogs.com/CoderEYLee/p/Object-C-0027.html
Copyright © 2011-2022 走看看