zoukankan      html  css  js  c++  java
  • 通过runtime打印出对象所有属性的值

    通过runtime打印出对象所有属性的值

    今天给给大家提供的关于NSObject的category,通过runtime打印属性的值,相当有用哦,以后你再也不用每个对象都通过NSLog来逐个打印属性值了。

    源码:

    NSObject+Properties.h 与 NSObject+Properties.m

    //
    //  NSObject+Properties.h
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface NSObject (Properties)
    
    - (NSDictionary*)propertiesValues;
    
    @end
    //
    //  NSObject+Properties.m
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "NSObject+Properties.h"
    #import <objc/runtime.h>
    
    @implementation NSObject (Properties)
    
    - (NSDictionary*)propertiesValues
    {
        // 获取属性列表
        NSArray *properties = [self propertyNames:[self class]];
        
        // 根据属性列表获取属性值
        return [self propertiesAndValuesDictionary:self properties:properties];
    }
    
    #pragma private - 私有方法
    
    // 获取一个类的属性名字列表
    - (NSArray*)propertyNames:(Class)class
    {
        NSMutableArray  *propertyNames = [[NSMutableArray alloc] init];
        unsigned int     propertyCount = 0;
        objc_property_t *properties    = class_copyPropertyList(class, &propertyCount);
        
        for (unsigned int i = 0; i < propertyCount; ++i)
        {
            objc_property_t  property = properties[i];
            const char      *name     = property_getName(property);
            
            [propertyNames addObject:[NSString stringWithUTF8String:name]];
        }
        
        free(properties);
        
        return propertyNames;
    }
    
    // 根据属性数组获取该属性的值
    - (NSDictionary*)propertiesAndValuesDictionary:(id)obj properties:(NSArray *)properties
    {
        NSMutableDictionary *propertiesValuesDic = [NSMutableDictionary dictionary];
        
        for (NSString *property in properties)
        {
            SEL getSel = NSSelectorFromString(property);
            
            if ([obj respondsToSelector:getSel])
            {
                NSMethodSignature  *signature  = nil;
                signature                      = [obj methodSignatureForSelector:getSel];
                NSInvocation       *invocation = [NSInvocation invocationWithMethodSignature:signature];
                [invocation setTarget:obj];
                [invocation setSelector:getSel];
                NSObject * __unsafe_unretained valueObj = nil;
                [invocation invoke];
                [invocation getReturnValue:&valueObj];
                
                //assign to @"" string
                if (valueObj == nil)
                {
                    valueObj = @"";
                }
                
                propertiesValuesDic[property] = valueObj;
            }
        }
        
        return propertiesValuesDic;
    }
    
    @end
    //
    //  NSObject+Properties.m
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "NSObject+Properties.h"
    #import <objc/runtime.h>
    
    @implementation NSObject (Properties)
    
    - (NSDictionary*)propertiesValues
    {
        // 获取属性列表
        NSArray *properties = [self propertyNames:[self class]];
        
        // 根据属性列表获取属性值
        return [self propertiesAndValuesDictionary:self properties:properties];
    }
    
    #pragma private - 私有方法
    
    // 获取一个类的属性名字列表
    - (NSArray*)propertyNames:(Class)class
    {
        NSMutableArray  *propertyNames = [[NSMutableArray alloc] init];
        unsigned int     propertyCount = 0;
        objc_property_t *properties    = class_copyPropertyList(class, &propertyCount);
        
        for (unsigned int i = 0; i < propertyCount; ++i)
        {
            objc_property_t  property = properties[i];
            const char      *name     = property_getName(property);
            
            [propertyNames addObject:[NSString stringWithUTF8String:name]];
        }
        
        free(properties);
        
        return propertyNames;
    }
    
    // 根据属性数组获取该属性的值
    - (NSDictionary*)propertiesAndValuesDictionary:(id)obj properties:(NSArray *)properties
    {
        NSMutableDictionary *propertiesValuesDic = [NSMutableDictionary dictionary];
        
        for (NSString *property in properties)
        {
            SEL getSel = NSSelectorFromString(property);
            
            if ([obj respondsToSelector:getSel])
            {
                NSMethodSignature  *signature  = nil;
                signature                      = [obj methodSignatureForSelector:getSel];
                NSInvocation       *invocation = [NSInvocation invocationWithMethodSignature:signature];
                [invocation setTarget:obj];
                [invocation setSelector:getSel];
                NSObject * __unsafe_unretained valueObj = nil;
                [invocation invoke];
                [invocation getReturnValue:&valueObj];
                
                //assign to @"" string
                if (valueObj == nil)
                {
                    valueObj = @"";
                }
                
                propertiesValuesDic[property] = valueObj;
            }
        }
        
        return propertiesValuesDic;
    }
    
    @end

    使用详情:

    测试用Model

    //
    //  YXModel.h
    //  Test
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface YXModel : NSObject
    
    @property (nonatomic, strong) NSString      *name;
    @property (nonatomic, strong) NSNumber      *age;
    @property (nonatomic, strong) NSString      *sex;
    @property (nonatomic, strong) NSDictionary  *info;
    
    @end
    //
    //  YXModel.m
    //  Test
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "YXModel.h"
    
    @implementation YXModel
    
    @end

    使用:

    //
    //  AppDelegate.m
    //  Test
    //
    //  Created by YouXianMing on 14-9-4.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "NSObject+Properties.h"
    #import "YXModel.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 初始化model
        YXModel *model = [YXModel new];
        
        // 赋值
        model.name     = @"YouXianMing";
        model.age      = @26;
        model.sex      = @"male";
        model.info     = @{@"A":@"B", @"C":@"D"};
        
        // 打印出所有属性的值
        NSLog(@"%@", model.propertiesValues);
        
        return YES;
    }
    
    @end

    打印信息:

    2014-09-04 19:39:08.913 Test[1278:60b] {

        age = 26;

        info =     {

            A = B;

            C = D;

        };

        name = YouXianMing;

        sex = male;

    }

  • 相关阅读:
    打印图片的属性和实现另存图片功能以及使用numpy
    opencv3.3 安装环境教程以及实现个图片读取功能
    在linux 创建网络会话和绑定两块网卡
    安装yum仓库
    C#添加错误日志信息
    关于使用宏将csv批量转换成xls的分享
    C#关于MySQL中文乱码问题
    无法启动iis express web服务器解决
    关于关闭WPS锁屏屏保及设置电脑自动关闭显示屏及休眠的分享
    为什么QQ能上却打不开网页呢?
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3956777.html
Copyright © 2011-2022 走看看