遍歷屬性
#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];
}