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,230评论 0 7
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,762评论 0 9
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 1,431评论 0 8
  • Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的...
    有一种再见叫青春阅读 606评论 0 3
  • 昨天收拾书橱,发现了一摞儿子的小作文本,随手翻开,目光就再也没有离开…… 1\三年级上学期:我的小鸡在我上一年级的...
    洁语落笔尖阅读 496评论 7 8