Objective-C OC通过runtime反射获取变量和属性

本文主要阐述Objective-C如何通过runtime反射获取变量和属性并获取相应的值
反射获取方法以及静态方法以及调用请参考https://www.jianshu.com/p/02b506888450
我们知道很多语言可以通过反射获取对象的方法,属性、变量等,Objective-C通过Runtime也是可以实现的。

首先定义一个基类以及子类

基类头文件
#import 

NS_ASSUME_NONNULL_BEGIN

@interface DlibBaseClass : NSObject
@property(nonatomic,copy) NSString * abaseClassString;
@end

NS_ASSUME_NONNULL_END
基类实现文件
#import "DlibBaseClass.h"

@implementation DlibBaseClass
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.abaseClassString=@"i'm DlibBaseClass";
    }
    return self;
}
@end

子类头文件
NS_ASSUME_NONNULL_BEGIN

@interface DlibClass : DlibBaseClass
@property(nonatomic,assign)NSInteger aInt;
@property(nonatomic,copy)NSString *aString;
@end

NS_ASSUME_NONNULL_END
子类实现文件
#import "DlibClass.h"

@implementation DlibClass
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.aInt=100;
        self.aString=@"i'm DlibClass";
    }
    return self;
}
@end

通过Runtime获取所有变量以及值。

   Class currentClass=NSClassFromString(@"DlibClass");
   
    id  object=[[currentClass alloc] init];
    NSString *key=nil;
    do{
        NSLog(@"------------------Class is %@-----------------",currentClass);
        NSLog(@"Class pointer %p",currentClass);
        unsigned int numIvars;
        Ivar *vars = class_copyIvarList(currentClass, &numIvars);
        unsigned int i = 0;
        for (; i < numIvars; i++) {
            Ivar thisIvar = vars[i];
            key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
            NSLog(@"variable_name :%@", key);
            id value = [object valueForKey:key];
            NSLog(@"variable_value :%@",value);
            if([key isEqualToString:@"isa"]){
                NSLog(@"Class pointer %p",value);
            }
        }
        
        free(vars);
        currentClass = class_getSuperclass(currentClass);
        
        
    }while(currentClass);

代码执行结果

大家可以留意一下打印,我们的通过属性定义的变量都是以_开头,而打到NSObject的时候,NSObject只有一个变量而且变量名叫isa指向的是DlibClass。也就是[[currentClass alloc] init]中的currentClass。

2020-12-01 11:30:45.802370+0800 kingViewTest[2781:56445] ------------------Class is DlibClass-----------------
2020-12-01 11:30:45.802524+0800 kingViewTest[2781:56445] Class pointer 0x104a3d670
2020-12-01 11:30:46.423899+0800 kingViewTest[2781:56445] variable_name :_aInt
2020-12-01 11:30:46.424166+0800 kingViewTest[2781:56445] variable_value :100
2020-12-01 11:30:46.424266+0800 kingViewTest[2781:56445] variable_name :_aString
2020-12-01 11:30:46.424420+0800 kingViewTest[2781:56445] variable_value :i'm DlibClass
2020-12-01 11:30:46.424532+0800 kingViewTest[2781:56445] ------------------Class is DlibBaseClass-----------------
2020-12-01 11:30:46.424611+0800 kingViewTest[2781:56445] Class pointer 0x104a3d5d0
2020-12-01 11:30:48.715061+0800 kingViewTest[2781:56445] variable_name :_abaseClassString
2020-12-01 11:30:48.715340+0800 kingViewTest[2781:56445] variable_value :i'm DlibBaseClass
2020-12-01 11:30:48.715465+0800 kingViewTest[2781:56445] ------------------Class is NSObject-----------------
2020-12-01 11:30:48.715564+0800 kingViewTest[2781:56445] Class pointer 0x7fff89c1ed00
2020-12-01 11:30:49.137825+0800 kingViewTest[2781:56445] variable_name :isa
2020-12-01 11:30:49.138100+0800 kingViewTest[2781:56445] variable_value :DlibClass
2020-12-01 11:30:49.385176+0800 kingViewTest[2781:56445] Class pointer 0x104a3d670

通过Runtime获取所有属性以及值。

Class currentClass=NSClassFromString(@"DlibClass");
   
    id  object=[[currentClass alloc] init];

    NSString *key=nil;
    do{
        NSLog(@"------------------Class is %@-----------------",currentClass);
        NSLog(@"Class pointer %p",currentClass);
        
        unsigned int count;
        
        objc_property_t *propertyList = class_copyPropertyList(currentClass, &count);

        unsigned int i = 0;
        for (; i < count; i++) {
            const char *propertyName = property_getName(propertyList[i]);
            key = [NSString stringWithUTF8String:propertyName];
            NSLog(@"property_name :%@", key);
            if([key isEqualToString:@"observationInfo"]){
                continue;
            }
            id value = [object valueForKey:key];
            NSLog(@"property_value :%@",value);
            
        }
        
       free(propertyList);
        currentClass = class_getSuperclass(currentClass);
        
        
    }while(currentClass);

代码执行结果

大家可以看到NSObject的属性还是比较多的,具体用途可以查一下相关资料。

2020-12-01 11:53:30.037083+0800 kingViewTest[3282:69028] ------------------Class is DlibClass-----------------
2020-12-01 11:53:30.037229+0800 kingViewTest[3282:69028] Class pointer 0x10a40f670
2020-12-01 11:53:30.037338+0800 kingViewTest[3282:69028] property_name :aInt
2020-12-01 11:53:30.037459+0800 kingViewTest[3282:69028] property_value :100
2020-12-01 11:53:30.037543+0800 kingViewTest[3282:69028] property_name :aString
2020-12-01 11:53:30.037640+0800 kingViewTest[3282:69028] property_value :i'm DlibClass
2020-12-01 11:53:30.037745+0800 kingViewTest[3282:69028] ------------------Class is DlibBaseClass-----------------
2020-12-01 11:53:30.037821+0800 kingViewTest[3282:69028] Class pointer 0x10a40f5d0
2020-12-01 11:53:30.037902+0800 kingViewTest[3282:69028] property_name :abaseClassString
2020-12-01 11:53:30.038110+0800 kingViewTest[3282:69028] property_value :i'm DlibBaseClass
2020-12-01 11:53:30.038406+0800 kingViewTest[3282:69028] ------------------Class is NSObject-----------------
2020-12-01 11:53:30.038729+0800 kingViewTest[3282:69028] Class pointer 0x7fff89c1ed00
2020-12-01 11:53:30.039081+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.039448+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.039641+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.045889+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.045985+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.046114+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.046204+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.046306+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.046378+0800 kingViewTest[3282:69028] property_name :isAccessibilityElement
2020-12-01 11:53:30.046477+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.046563+0800 kingViewTest[3282:69028] property_name :accessibilityLabel
2020-12-01 11:53:30.046657+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.046741+0800 kingViewTest[3282:69028] property_name :accessibilityAttributedLabel
2020-12-01 11:53:30.046930+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.047159+0800 kingViewTest[3282:69028] property_name :accessibilityHint
2020-12-01 11:53:30.047461+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.047653+0800 kingViewTest[3282:69028] property_name :accessibilityAttributedHint
2020-12-01 11:53:30.047943+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.048284+0800 kingViewTest[3282:69028] property_name :accessibilityValue
2020-12-01 11:53:30.059623+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.059732+0800 kingViewTest[3282:69028] property_name :accessibilityAttributedValue
2020-12-01 11:53:30.059820+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.059889+0800 kingViewTest[3282:69028] property_name :accessibilityTraits
2020-12-01 11:53:30.059977+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.060067+0800 kingViewTest[3282:69028] property_name :accessibilityFrame
2020-12-01 11:53:30.060212+0800 kingViewTest[3282:69028] property_value :NSRect: {{0, 0}, {0, 0}}
2020-12-01 11:53:30.060295+0800 kingViewTest[3282:69028] property_name :accessibilityPath
2020-12-01 11:53:30.060489+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.060763+0800 kingViewTest[3282:69028] property_name :accessibilityActivationPoint
2020-12-01 11:53:30.061094+0800 kingViewTest[3282:69028] property_value :NSPoint: {0, 0}
2020-12-01 11:53:30.061332+0800 kingViewTest[3282:69028] property_name :accessibilityLanguage
2020-12-01 11:53:30.061614+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.061821+0800 kingViewTest[3282:69028] property_name :accessibilityElementsHidden
2020-12-01 11:53:30.062094+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.062313+0800 kingViewTest[3282:69028] property_name :accessibilityViewIsModal
2020-12-01 11:53:30.062567+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.062809+0800 kingViewTest[3282:69028] property_name :shouldGroupAccessibilityChildren
2020-12-01 11:53:30.063066+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.063328+0800 kingViewTest[3282:69028] property_name :accessibilityNavigationStyle
2020-12-01 11:53:30.063535+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.063759+0800 kingViewTest[3282:69028] property_name :accessibilityRespondsToUserInteraction
2020-12-01 11:53:30.064044+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.064344+0800 kingViewTest[3282:69028] property_name :accessibilityUserInputLabels
2020-12-01 11:53:30.064631+0800 kingViewTest[3282:69028] property_value :(
)
2020-12-01 11:53:30.064893+0800 kingViewTest[3282:69028] property_name :accessibilityAttributedUserInputLabels
2020-12-01 11:53:30.065140+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.065428+0800 kingViewTest[3282:69028] property_name :accessibilityHeaderElements
2020-12-01 11:53:30.065626+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.065853+0800 kingViewTest[3282:69028] property_name :accessibilityTextualContext
2020-12-01 11:53:30.066113+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.066353+0800 kingViewTest[3282:69028] property_name :accessibilityCustomActions
2020-12-01 11:53:30.066613+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.066800+0800 kingViewTest[3282:69028] property_name :accessibilityIdentifier
2020-12-01 11:53:30.067042+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.067260+0800 kingViewTest[3282:69028] property_name :accessibilityLocalizedStringKey
2020-12-01 11:53:30.067523+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.067780+0800 kingViewTest[3282:69028] property_name :accessibilityElements
2020-12-01 11:53:30.068065+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.068274+0800 kingViewTest[3282:69028] property_name :accessibilityContainerType
2020-12-01 11:53:30.068532+0800 kingViewTest[3282:69028] property_value :0
2020-12-01 11:53:30.068744+0800 kingViewTest[3282:69028] property_name :accessibilityCustomRotors
2020-12-01 11:53:30.068993+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.069259+0800 kingViewTest[3282:69028] property_name :traitStorageList
2020-12-01 11:53:30.069578+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.069879+0800 kingViewTest[3282:69028] property_name :_ui_descriptionBuilder
2020-12-01 11:53:30.070248+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.070539+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.070821+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.071095+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.071404+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.071768+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.072088+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.072740+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.073149+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.073442+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.073697+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.073970+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.074175+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.074400+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.074682+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.074859+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.075098+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.075368+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.075614+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.075855+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.076106+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.076332+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.076601+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.076795+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.077045+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.077269+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.077510+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.077752+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.077993+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.078224+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.078486+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.078719+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.078984+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.079266+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.079545+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.079821+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.080085+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.080293+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.080541+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.080788+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.081066+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.081312+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.081552+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.081793+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.082033+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.082274+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.082507+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.082754+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.083016+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.083179+0800 kingViewTest[3282:69028] property_name :observationInfo
2020-12-01 11:53:30.083437+0800 kingViewTest[3282:69028] property_name :classForKeyedArchiver
2020-12-01 11:53:30.083711+0800 kingViewTest[3282:69028] property_value :DlibClass
2020-12-01 11:53:30.083926+0800 kingViewTest[3282:69028] property_name :autoContentAccessingProxy
2020-12-01 11:53:30.084210+0800 kingViewTest[3282:69028] property_value :(null)
2020-12-01 11:53:30.084442+0800 kingViewTest[3282:69028] property_name :hash
2020-12-01 11:53:30.084700+0800 kingViewTest[3282:69028] property_value :105553131762592
2020-12-01 11:53:30.084951+0800 kingViewTest[3282:69028] property_name :superclass
2020-12-01 11:53:30.085196+0800 kingViewTest[3282:69028] property_value :DlibBaseClass
2020-12-01 11:53:30.085439+0800 kingViewTest[3282:69028] property_name :description
2020-12-01 11:53:30.085663+0800 kingViewTest[3282:69028] property_value :
2020-12-01 11:53:30.085906+0800 kingViewTest[3282:69028] property_name :debugDescription
2020-12-01 11:53:30.086139+0800 kingViewTest[3282:69028] property_value :

你可能感兴趣的:(Objective-C OC通过runtime反射获取变量和属性)