如何通過 Runtime 獲取對象的屬性

Runtime 的應(yīng)用場景:
關(guān)聯(lián)對象:

  • (1).仿照 SDWebImage
    - <1>給分類動態(tài)添加屬性;
    - <2>解耦;
    - <3>簡化使用;

  • (2).動態(tài)獲取類的屬性:
    - <1>字典 -->模型;
    - <2>建立 NSObject 分類;
    - <3>1.class_copyPropertyList--> 獲取 Person 類.h 文件中的幾個屬性;
    - <4>遍歷數(shù)組,用的不是 for - in 而是 for 循環(huán) ;
    - <5>獲得 Person 類. h 文件中的幾個屬性的名稱用 getName方法:property_getName(這里放的是:"屬性列表的數(shù)組") ;具體代碼中會有體現(xiàn)!!!
    - <6>與字典轉(zhuǎn)模型過程一樣,創(chuàng)建可變數(shù)組并把名字添加到這個可變數(shù)組中 ;
    - <7>釋放數(shù)組: free(propertyList) ;否則會有內(nèi)存問題!!!

主 Bundle 欄

Snip20161211_7.png

ViewController.m 文件

#import "ViewController.h"
#import "Person.h"  //繼承自NSObject
#import "NSObject+Runtime.h"    //NSObject的分類
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //獲取 Person 類的屬性數(shù)組:
    NSArray *propertiesArray = [Person MD_objProperties] ;
    NSLog(@"%@" , propertiesArray) ;
}

@end

Person.h 文件

#import <Foundation/Foundation.h>

@interface Person : NSObject
/**
 *  姓名:
 */
@property (nonatomic, copy) NSString *name ;
/**
 *  年齡
 */
@property (nonatomic) NSInteger age ;

/**
 * title:
 */
@property (nonatomic, copy) NSString *title ;

/**
 *  高度:
 */
@property (nonatomic) double height ;

@end

Person.m 文件

#import "Person.h"

@implementation Person

//什么也沒有~空的!!!

@end

NSObject+Runtime.h 文件(給 NSObject 創(chuàng)建的分類)

#import <Foundation/Foundation.h>

@interface NSObject (Runtime)
/**
 *  獲取累的屬性列表數(shù)組:
 *
 *  @return 累的屬性列表數(shù)組
 */
//創(chuàng)建類方法,該類方法返回的數(shù)據(jù)類型為一個數(shù)組:
+ (NSArray *)MD_objProperties ;

@end

NSObject+Runtime.m 文件

#import "NSObject+Runtime.h"
#import <objc/runtime.h>
@implementation NSObject (Runtime)

//該類方法返回的數(shù)據(jù)類型為包含一個字符串內(nèi)容為: hello 的數(shù)組:
+ (NSArray *)MD_objProperties {
    
    //調(diào)用運行時 runtime 取得 Person 類的屬性列表:
    //Ivar:成員變量:
//    class_copyIvarList(__unsafe_unretained Class cls, unsigned int *outCount)
    //Method:成員方法:
//    class_copyMethodList(__unsafe_unretained Class cls, unsigned int *outCount)
    //Property:屬性:
//    class_copyPropertyList(__unsafe_unretained Class cls, unsigned int *outCount)
    //protocol: 協(xié)議:
//    class_copyProtocolList(__unsafe_unretained Class cls, unsigned int *outCount)
    
    /**
     *  參數(shù):
     1.__unsafe_unretained Class cls :要獲取的類 ;
     2.unsigned int *outCount :類屬性的個數(shù)的指針 ;
     *
     3.返回值為:所有屬性的數(shù)組 ;
     運行時 Runtime 是 C 語言的概念:
     在 C 語言中數(shù)組的名字就是指向數(shù)組中第一個元素的地址!
     */
    
    //數(shù)組的數(shù)量:
    unsigned int count = 0 ;
    
    objc_property_t *propertyList = class_copyPropertyList([self class], &count) ;
    
    NSLog(@"屬性的數(shù)量:%@" , [self class]) ;
    NSLog(@"屬性的數(shù)量:%d" , count) ;
    
    //創(chuàng)建可變數(shù)組:
    NSMutableArray *mArray = [NSMutableArray array] ;
    
    //遍歷所有屬性:
    for (unsigned int i = 0; i < count; i++) {
        
        //1.從數(shù)組中取得所有屬性:
        //注意這里不要再加 * 號,點進(jìn)會發(fā)現(xiàn)他是一個結(jié)構(gòu)體指針!
        objc_property_t pty = propertyList[i] ;
        
        //2.獲得屬性的名稱:
        //這是一個 C 語言的字符串:
        const char *cName = property_getName(pty) ;
        
//        NSLog(@"%s" , cName) ;
        
        //C 語言字符串轉(zhuǎn)成 OC 字符串:
        //    NSUTF8StringEncoding = 4:
        NSString *ocString = [NSString stringWithCString:cName encoding:4] ;
        
//        NSLog(@"%@" , ocString) ;
        
        //將屬性名稱添加到數(shù)組:
        [mArray addObject:ocString] ;
        
    }
    
    //一定要釋放數(shù)組:!!!!!!!!
    free(propertyList) ;
    
    return mArray.copy ;
    
}

@end

熱愛開源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容