遍历属性
#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];
}
网友评论