Runtime 獲取一個類的所有的屬性、成員變量、方法

這篇文章是寫的一些關于 Runtime 的用法總結,主要的是一個引導作用,如果想要在項目中去使用,還需要對具體項目做出不同的處理,但是思路都是差不多的。

本篇文章算是一個入門,獲取一個類的所有的屬性、成員變量、方法

1.首先創建一個繼承于NSObject的類:Coder,然后我們給他一些屬性,成員變量,方法等:

//Coder.h
#import <Foundation/Foundation.h>

@interfaceCoder :NSObject
{
  NSString * _address;
  NSString * _code;
}

@property(nonatomic,copy) NSString * name;
@property(nonatomic,assign) NSInteger age;

/**
獲取所有的屬性
*/
- (NSDictionary *)getProperties;

/**
獲取所有的成員變量
*/
- (NSDictionary *)getIvars;

/**
獲取所有的方法
*/
- (NSDictionary *)getMethods;

@end

2.做完這些,我們去實現它的各個方法,上代碼:

//Coder.m
#import "Coder.h"
#import <objc/runtime.h>

@implementation Coder

// 獲取所有的屬性
- (NSDictionary *)getProperties {
    
    unsigned int count = 0;
    
    // 獲取類的所有 Property,如果沒有屬性 count 就為0
    objc_property_t * properties = class_copyPropertyList([self class], &count);
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    
    for (NSUInteger i = 0; i < count; i ++) {
        
        // 獲取屬性的 Name 和 value
        const char * propertyName = property_getName(properties[i]);
        NSString * name = [NSString stringWithUTF8String:propertyName];
        id propertyValue = [self valueForKey:name];
        
        if (propertyValue) {
            dict[name] = propertyValue;
        } else {
            dict[name] = @"value為nil";
        }
    }
    // properties是一個數組指針,需要使用free函數來釋放內存。
    free(properties);
    
    return dict;
}

// 獲取所有的成員變量
- (NSDictionary *)getIvars {
    unsigned int count = 0;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    // 獲取類的所有 Ivar
    Ivar *ivars = class_copyIvarList([self class], &count);
    
    for (NSUInteger i = 0; i < count; i ++) {
        // 獲取成員變量的 Name 和 value
        const char *varName = ivar_getName(ivars[i]);
        NSString *name = [NSString stringWithUTF8String:varName];
        id varValue = [self valueForKey:name];
        
        if (varValue) {
            dict[name] = varValue;
        } else {
            dict[name] = @"value為nil";
        }
        
    }
    
    free(ivars);
    
    return dict;
}

// 獲取所有的方法
- (NSDictionary *)getMethods {
    unsigned int count = 0;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    // 獲取類的所有 Method
    Method *methods = class_copyMethodList([self class], &count);
    
    for (NSUInteger i = 0; i < count; i ++) {
        
        // 獲取方法 Name
        SEL methodSEL = method_getName(methods[i]);
        const char *methodName = sel_getName(methodSEL);
        NSString *name = [NSString stringWithUTF8String:methodName];
        
        // 獲取方法的參數列表
        int arguments = method_getNumberOfArguments(methods[i]);
        //因為消息發送的時候會有兩個默認的參數(消息接受者和方法名),所以需要減去2
        dict[name] = @(arguments-2);
    }
    
    free(methods);
    
    return dict;
}

3.接下來,就可以去一個一個的去獲得我們想要的了:

- (void)viewDidLoad {
    [super viewDidLoad];

    Coder * coder = [[Coder alloc]init];
    coder.name = @"IAMCJ";
    coder.age = 18;
    [coder setValue:@"北京" forKey:@"_address"];//_address/address均可
    
    // 獲取所有的屬性
    NSDictionary *propertyDict = [coder getProperties];
    for (NSString *propertyName in propertyDict.allKeys) {
        NSLog(@"propertyName:%@, propertyValue:%@",propertyName, propertyDict[propertyName]);
    }
    
    // 獲取所有的成員變量
    NSDictionary *ivarDict = [coder getIvars];
    for (NSString *ivarName in ivarDict.allKeys) {
        NSLog(@"ivarName:%@, ivarValue:%@",ivarName, ivarDict[ivarName]);
    }
    
    // 獲取所有的方法
    NSDictionary *methodDict = [coder getMethods];
    for (NSString *methodName in methodDict.allKeys) {
        NSLog(@"methodName:%@, argumentsCount:%@", methodName, methodDict[methodName]);
    }
}

4.這是打印出來的內容:

2017-06-16 11:34:51.414 GetPropertyIvarMethod[2853:49972] propertyName:name, propertyValue:IAMCJ
2017-06-16 11:34:51.414 GetPropertyIvarMethod[2853:49972] propertyName:age, propertyValue:18
2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_name, ivarValue:IAMCJ
2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_age, ivarValue:18
2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_address, ivarValue:北京
2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] ivarName:_code, ivarValue:value為nil
2017-06-16 11:34:51.415 GetPropertyIvarMethod[2853:49972] methodName:setName:, argumentsCount:1
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:age, argumentsCount:0
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getIvars, argumentsCount:0
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getMethods, argumentsCount:0
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:setAge:, argumentsCount:1
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:getProperties, argumentsCount:0
2017-06-16 11:34:51.416 GetPropertyIvarMethod[2853:49972] methodName:.cxx_destruct, argumentsCount:0
2017-06-16 11:34:51.417 GetPropertyIvarMethod[2853:49972] methodName:name, argumentsCount:0

從上面我們可以看到成員變量的個數比屬性多兩個,其實這就是
class_copyPropertyList 和 class_copyIvarList 的區別,前者是獲取所有的屬性列表,后者是獲取所有的成員變量列表,如果你想問 Coder 這個類的屬性有兩個,成員變量不也是兩個嗎?其實每個屬性會自動生成一個 _XXX 的成員變量,并自動生成 setter 和 getter 方法,從上面的打印內容你可以清晰的看出來。
注:方法列表里面多了一個 .cxx_destruct 方法,你可能不知道這個方法是干什么的,其實 .cxx_destruct 方法原本是為了 C++ 對象析構的,在這里 ARC 借用了這個方法插入代碼實現了自動內存釋放的工作。想深入了解的同學點這里.

這里的內容是為了讓你去理解這個東西,但是你可能還不知道可以用它干什么,想了解一下可以用這個做一些什么事情,看一下iOS利用Runtime自定義控制器POP手勢動畫,如果在此之前內沒用過此類方法解決問題,看完之后你會原來還可以這樣,666。

我會再寫幾篇關于Runtime的用法的文章,同時也在提升自己的能力,項目比較忙,所以更新時間不定,水平有限,有錯誤望指出。Demo在這里,不麻煩的話 star 一下,感謝大家。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容