runtime-獲取一個類的所有成員變量(屬性)\所有方法

遍歷屬性

#import <Foundation/Foundation.h>

@interface StudentProperty :NSObject

{

NSString*_name;

float _height;

}

@property(nonatomic,copy,readonly)NSString*sex;

@property(nonatomic,copy)NSString*lanou;

@property(nonatomic,assign)int age;

@property(nonatomic,copy)NSString*phNum;

+ (void)test;

@end

#import "StudentProperty.h"

#import <objc/runtime.h>

@implementation StudentProperty

+ (void)test {

StudentProperty*sp = [[StudentProperty alloc]init];

[sp getAllProperty]; // 獲取所有屬性

//[sp getAllIvar]; // 獲取所有成員變量?

}

// 獲取所有屬性

- (void)getAllProperty {

unsigned int outCount =0;

//屬性列表

objc_property_t *propertys = class_copyPropertyList(self.class, &outCount);

for(unsigned int i =0; i < outCount; ++i) {

objc_property_t ?property = propertys[i];

//屬性名

constchar*propertyName =property_getName(property);

//屬性類型@encode編碼

const char *propertyAttName =property_getAttributes(property);

NSLog(@"%s %s", propertyName,propertyAttName);

unsigned int outCount2 =0;

//語義特性列表

objc_property_attribute_t ?* propertyAtts =property_copyAttributeList(property,&outCount2);

for(unsigned int i=0;i < outCount; ++i) {

objc_property_attribute_t properAtt = propertyAtts[i];

//語義特性名

const char *name = properAtt.name;

//語義特性值

const char *value = properAtt.value;

NSLog(@"attName:%sattValue:%s",name,value);

}

free(propertyAtts);//需要手動釋放

}

free(propertys);

}

遍歷所有成員變量

//獲取所有的成員變量

- (void)getAllIvar {

unsigned int outCount =0;

Ivar*ivars =class_copyIvarList(self.class, &outCount);

for(unsigned int i =0; i < outCount; ++i) {

Ivar ivar = ivars[i];

constchar*ivarName =ivar_getName(ivar);

constchar*ivarEncoder =ivar_getTypeEncoding(ivar);

NSLog(@"Ivar name:%s Ivar TypeEncoder:%s",ivarName,ivarEncoder);

? ? ?}

free(ivars);

}

#import "ViewController.h"

#import "StudentProperty.h"

- (void)viewDidLoad {

[superview DidLoad];

[StudentProperty test];

}

遍歷所有屬性如下:


遍歷所有成員變量如下:

遍歷所有方法

#import <Foundation/Foundation.h>

@interface StudentMethodList :NSObject

+ (void)test;

@end

#import"StudentMethodList.h"

#import <objc/runtime.h>

@implementation StudentMethodList

+ (void)test

{

StudentMethodList*sm = [[StudentMethodList alloc] init];

[sm getAllMethodList];

}

- (void)getAllMethodList {

unsigned int outCount =0;

Method*methods =class_copyMethodList(self.class, &outCount);

for(unsigned int i =0; i < outCount; ++i) {

Method method = methods[i];

SEL methodName =method_getName(method);

NSLog(@"方法名:%@",NSStringFromSelector(methodName));

unsigned int argCount =method_getNumberOfArguments(method);

char dst[1024];// C語言數組

for(unsigned int i =0; i < argCount; ++i) {

//獲取所有參數的類型Type

method_getArgumentType(method, i, dst,1024);

NSLog(@"第%d個參數的類型為:%s",i,dst);

}

char dst2[1024];

method_getReturnType(method, dst2,1024);

NSLog(@"返回值類型:%s",dst2);

}

}

#pragma mark -測試方法

- (void)test:(NSString*)str1 str2:(NSNumber*)str2{

NSLog(@"兩個參數");

}

- (int)test2{

return0;

}

- (constvoid*)test3{

return"111111";

}

@end

#import "ViewController.h"

#import "StudentMethodList.h"

@implementation ViewController

- (void)viewDidLoad {

[superview DidLoad];

[StudentMethodList test];

}


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

推薦閱讀更多精彩內容

  • 我們常常會聽說 Objective-C 是一門動態語言,那么這個「動態」表現在哪呢?我想最主要的表現就是 Obje...
    Ethan_Struggle閱讀 2,232評論 0 7
  • 轉至元數據結尾創建: 董瀟偉,最新修改于: 十二月 23, 2016 轉至元數據起始第一章:isa和Class一....
    40c0490e5268閱讀 1,776評論 0 9
  • Objective-C語言是一門動態語言,他將很多靜態語言在編譯和鏈接時期做的事情放到了運行時來處理。這種動態語言...
    tigger丨閱讀 1,439評論 0 8
  • Objective-C語言是一門動態語言,它將很多靜態語言在編譯和鏈接時期做的事放到了運行時來處理。這種動態語言的...
    有一種再見叫青春閱讀 611評論 0 3
  • 昨天收拾書櫥,發現了一摞兒子的小作文本,隨手翻開,目光就再也沒有離開…… 1\三年級上學期:我的小雞在我上一年級的...
    潔語落筆尖閱讀 499評論 7 8