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

遍歷屬性

#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;

//語(yǔ)義特性列表

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

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

objc_property_attribute_t properAtt = propertyAtts[i];

//語(yǔ)義特性名

const char *name = properAtt.name;

//語(yǔ)義特性值

const char *value = properAtt.value;

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

}

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

}

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語(yǔ)言數(shù)組

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

//獲取所有參數(shù)的類型Type

method_getArgumentType(method, i, dst,1024);

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

}

char dst2[1024];

method_getReturnType(method, dst2,1024);

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

}

}

#pragma mark -測(cè)試方法

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

NSLog(@"兩個(gè)參數(shù)");

}

- (int)test2{

return0;

}

- (constvoid*)test3{

return"111111";

}

@end

#import "ViewController.h"

#import "StudentMethodList.h"

@implementation ViewController

- (void)viewDidLoad {

[superview DidLoad];

[StudentMethodList test];

}


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

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

  • 我們常常會(huì)聽說(shuō) Objective-C 是一門動(dòng)態(tài)語(yǔ)言,那么這個(gè)「動(dòng)態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,327評(píng)論 0 7
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評(píng)論 0 9
  • Objective-C語(yǔ)言是一門動(dòng)態(tài)語(yǔ)言,他將很多靜態(tài)語(yǔ)言在編譯和鏈接時(shí)期做的事情放到了運(yùn)行時(shí)來(lái)處理。這種動(dòng)態(tài)語(yǔ)言...
    tigger丨閱讀 1,582評(píng)論 0 8
  • Objective-C語(yǔ)言是一門動(dòng)態(tài)語(yǔ)言,它將很多靜態(tài)語(yǔ)言在編譯和鏈接時(shí)期做的事放到了運(yùn)行時(shí)來(lái)處理。這種動(dòng)態(tài)語(yǔ)言的...
    有一種再見叫青春閱讀 671評(píng)論 0 3
  • 昨天收拾書櫥,發(fā)現(xiàn)了一摞兒子的小作文本,隨手翻開,目光就再也沒有離開…… 1\三年級(jí)上學(xué)期:我的小雞在我上一年級(jí)的...
    潔語(yǔ)落筆尖閱讀 560評(píng)論 7 8

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