• iGson


    头文件

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    #import "NSString+Utils.h"
    
    @interface iGson : NSObject
    
    // 获取所有的property
    - (NSMutableArray *)getPropertyNames;
    
    // 获取成员属性及属性的类型等信息的列表
    - (NSMutableArray *)getPropertyAttrs;
    
    // 获取非ReadOnly的所有property
    - (NSMutableArray *)getNotReadOnlyPropertyNames;
    
    // 获取一个该类的实例需要多少字节的内存开销
    + (size_t)getInstanceMemoryCost;
    
    // 使用JSONKit做JSON解析
    // 用于JSON解析的辅助方法,方便对象赋值
    + (id)objFromDict:(NSDictionary *)dict;
    + (id)objFromJson:(NSString *)json;
    
    // 转换成json字符串
    - (NSString *)objToJson;
    - (NSDictionary *)ojbToDict;
    
    // 用于各个子类测试json解析
    - (void)testJsonParse;
    
    @end

    实现文件

    #import "iGson.h"
    #import "JSONKit.h"
    
    
    @interface NSString (Utils)
    - (BOOL)hasSubString:(NSString *)string;
    @end
    
    @implementation NSString (Utils)
    - (BOOL)hasSubString:(NSString *)string
    {
        NSRange range = [self rangeOfString:string];
        if(range.location == NSNotFound)
            return FALSE;
        return TRUE;
    }
    @end
    
    @interface iGson ()
    + (BOOL)isPropertyReadOnly:(objc_property_t)property;
    @end
    
    
    @implementation iGson
    
    + (BOOL)isPropertyReadOnly:(objc_property_t)property
    {
        NSString *propertyAttrString =  [NSString stringWithCString:property_getAttributes(property)
                                                           encoding:NSUTF8StringEncoding];
        
        
        if([propertyAttrString hasSubString: @",R,"]){
            return YES;
        }
        
        return FALSE;
    }
    
    
    - (NSMutableArray *)getPropertyAttrs
    {
        Class cls = [self class];
        NSMutableArray *propertyAttrsList = [[NSMutableArray alloc] init];
        
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                objc_property_t property = properties[i];
                //printf("%s
    " ,property_getAttributes(property));
                
                NSString *propertyAttrString =  [NSString stringWithCString:property_getAttributes(property)
                                                                   encoding:NSUTF8StringEncoding];
                //NSLog(@"propertyAttr: %@", propertyAttrString);
                
                // 字符串非空,则加入属性列表
                if(!(propertyAttrString == nil ||
                     propertyAttrString.length == 0 ||
                     [propertyAttrString isEqualToString:@""]))
                {
                    [propertyAttrsList addObject:propertyAttrString];
                }
            }
            
            [pool drain];
        }
        return [propertyAttrsList autorelease];
    }
    
    
    - (NSMutableArray *)getPropertyNames
    {
        Class cls = [self class];
        NSMutableArray *propertyList = [[NSMutableArray alloc] init];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                
                objc_property_t property = properties[i];
                //printf("%s
    ", property_getName(property));
                
                
                
                NSString *propertyString =  [NSString stringWithCString:property_getName(property)
                                                               encoding:NSUTF8StringEncoding];
                //NSLog(@"property: %@", propertyString);
                // 字符串非空,则加入属性列表
                if(!(propertyString == nil ||
                     propertyString.length == 0 ||
                     [propertyString isEqualToString:@""]))
                {
                    [propertyList addObject:propertyString];
                }
            }
            
            [pool drain];
        }
        return [propertyList autorelease];
    }
    
    - (NSMutableArray *)getNotReadOnlyPropertyNames
    {
        Class cls = [self class];
        NSMutableArray *propertyList = [[NSMutableArray alloc] init];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                
                objc_property_t property = properties[i];
                //printf("%s
    ", property_getName(property));
                
                if([Model isPropertyReadOnly:property]) continue;
                
                NSString *propertyString =  [NSString stringWithCString:property_getName(property)
                                                               encoding:NSUTF8StringEncoding];
                //NSLog(@"property: %@", propertyString);
                // 字符串非空,则加入属性列表
                if(!(propertyString == nil ||
                     propertyString.length == 0 ||
                     [propertyString isEqualToString:@""]))
                {
                    [propertyList addObject:propertyString];
                }
            }
            
            [pool drain];
        }
        return [propertyList autorelease];
        
    }
    
    + (size_t)getInstanceMemoryCost
    {
        return class_getInstanceSize([self class]);
    }
    
    
    + (id)objFromDict:(NSDictionary *)dict
    {
        id obj = [[[self class] alloc] init];
        
        NSMutableArray *properties = [obj getNotReadOnlyPropertyNames];
        for(NSString *property in properties){
            id objValue = [dict objectForKey:property];
            if([objValue isKindOfClass:[NSDictionary class]]){
                objValue = [[self class] objFromDict:objValue];
            }else if ([objValue isKindOfClass:[NSArray class]]){
                for(id itemObj in ((NSArray *)objValue)){
                    itemObj = [[self class] objFromDict:itemObj]; // 这句是否合理,需要测试下
                }
            }
            //判断非空再赋值
            if(objValue && (![objValue isKindOfClass:[NSNull class]])){
                [obj setValue:objValue forKey:property];
            }
            
        }
        
    #if __has_feature(objc_arc)
        return obj;
    #else
        return [obj autorelease];
    #endif
        
    }
    
    + (id)objFromJson:(NSString *)json
    {
        id dict = [json objectFromJSONString];
        return [[self class] objFromDict:dict];
    }
    
    - (NSDictionary *)objToDict
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSMutableArray *properties = [self getNotReadOnlyPropertyNames];
        for(NSString *property in properties){
            id obj = [self valueForKey:property];
            [dict setValue:obj forKey:property];
        }
        
        return dict;
    }
    
    
    - (NSString *)objToJson
    {
        NSString *json = nil;
        json = [[self objToDict] JSONString];
        return json;
    }
    
    - (NSString *)description
    {
        NSMutableString *s = [NSMutableString stringWithCapacity:0];
        
        [s appendFormat:@"
    Class: %@{
    ", NSStringFromClass([self class])];
        NSArray *arr = [self getNotReadOnlyPropertyNames];
        for(id o in arr){
            [s appendFormat:@"%@: %@
    ", o, [self valueForKey:o]];
        }
        [s appendFormat:@"}
    "];
        return s;
    }
    
    
    - (void)setNilValueForKey:(NSString *)key
    {
        // 暂时不做处理
    }
    
    - (void)testJsonParse
    {
        // 子类重载实现
    }
    
    @end


  • 相关阅读:
    Element ui TimePicker 开始时间不得小于结束时间
    Vue 使用print.js实现前端打印功能
    vue中props的默认写法
    Element Cascader 级联选择器 选择第一级 label标题选择 内容过多
    vue 定时器 不断切换组件 定时器越来越快的问题
    Vue 全局组件传递参数
    echarts渐变发光半圆仪表盘
    左侧折叠菜单在sessionStorage中保存左侧菜单的激活状态
    Vue 封装网络工具类
    .browserslistrc 配置兼容浏览器
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3503347.html
Copyright © 2020-2023  润新知