/// An opaque type that represents a method in a class definition. 描述类中的一个方法
typedef struct objc_method *Method;
/// An opaque type that represents an instance variable. 实例的变量
typedef struct objc_ivar *Ivar;
/// An opaque type that represents a category. 扩展
typedef struct objc_category *Category;
/// An opaque type that represents an Objective-C declared property. 描述成员属性
typedef struct objc_property *objc_property_t;
runtime中的表示
struct objc_class {
Class isa ;
#if !__OBJC2__
Class super_class ; //指向父类
const char *name ; //类名
long version ;
long info ;
long instance_size;
struct objc_ivar_list *ivars; //成员变量列表
struct objc_method_list **methodLists; //方法列表
struct objc_cache *cache ; //缓存
struct objc_protocol_list *protocols ; //协议列表
#endif
} OBJC2_UNAVAILABLE;
获取类的属性列表
unsigned int count;
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *propertyName = property_getName(propertyList[i]);
NSLog(@"proprety--->%@",[NSString stringWithUTF8String:propertyName]);
}
获取类的成员变量
unsigned int count;
//获取成员变量列表
Ivar *ivarList = class_copyIvarList([self class], &count);
for (unsigned int i; i<count; i++) {
Ivar ivar = ivarList[i];
const char *ivarname = ivar_getName(ivar);
NSLog(@"ivar----="">%@", [NSString stringWithUTF8String:ivarname]);
}
获取类的所有方法
//获取self的所有的方法
- (void)getMethods
{
unsigned int count;
Method *methodlist = class_copyMethodList([self class], &count);
for (int i = 0; i < count; i ++) {
Method method = methodlist[i];
NSLog(@"method---> %@",NSStringFromSelector(method_getName(method)));
}
}
获取类遵循的协议
//获取所有self类的协议
- (void)getProtocals
{
unsigned int count;
__unsafe_unretained Protocol **protocolLsit = class_copyProtocolList([self class], &count);
for (int i = 0; i < count; i++) {
Protocol *protocol = protocolLsit[i];
const char *protoalName = protocol_getName(protocol);
NSLog(@"protocol---> %@",[NSString stringWithUTF8String:protoalName]);
}
}