Objective-C中的分类允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以做到。
Objective-C中的协议是普遍存在的接口定义方式,即在一个类中通过@protocol定义接口,在另外类中实现接口,这种接口定义方式也成为“delegation”模式,@protocol声明了可以呗其他任何方法类实现的方法,协议仅仅是定义一个接口,而由其他的类去负责实现。
runtime对分类与协议的支持。
基础数据类型
typedef struct objc_category *Category;struct objc_category {char *category_name OBJC2_UNAVAILABLE; char *class_name OBJC2_UNAVAILABLE; struct objc_method_list *instance_methods OBJC2_UNAVAILABLE; struct objc_method_list *class_methods OBJC2_UNAVAILABLE; struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
}
这个结构体主要包含了分类定义的实例方法与类方法,其中instance_methods列表是objc_class中方法列表的一个子集,而class_methods列表是元类方法列表的一个子集。
typedef struct objc_object Protocol;
Protocol * objc_getProtocol ( const char *name );
Protocol ** objc_copyProtocolList ( unsigned int *outCount );
Protocol * objc_allocateProtocol ( const char *name );
void objc_registerProtocol ( Protocol *proto );
void protocol_addMethodDescription ( Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod );
void protocol_addProtocol ( Protocol *proto, Protocol *addition );
void protocol_addProperty ( Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty );
const char * protocol_getName ( Protocol *p );
BOOL protocol_isEqual ( Protocol *proto, Protocol *other );
struct objc_method_description * protocol_copyMethodDescriptionList ( Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount );
struct objc_method_description protocol_getMethodDescription ( Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod );
objc_property_t * protocol_copyPropertyList ( Protocol *proto, unsigned int *outCount );
objc_property_t protocol_getProperty ( Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty );
Protocol ** protocol_copyProtocolList ( Protocol *proto, unsigned int *outCount );
BOOL protocol_conformsToProtocol ( Protocol *proto, Protocol *other );
objc_getProtocol函数,需要注意的是如果仅仅是声明了一个协议,而未在任何类中实现这个协议,则该函数返回的是nil。
objc_copyProtocolList函数,获取到的数组需要使用free来释放
objc_allocateProtocol函数,如果同名的协议已经存在,则返回nil
objc_registerProtocol函数,创建一个新的协议后,必须调用该函数以在运行时中注册新的协议。协议注册后便可以使用,但不能再做修改,即注册完后不能再向协议添加方法或协议
需要强调的是,协议一旦注册后就不可再修改,即无法再通过调用protocol_addMethodDescription、protocol_addProtocol和protocol_addProperty往协议中添加方法等。
看一个分类小例子,自定义类和分类,并调用运行时代码获取其方法列表:
#import @interface RuntimeCategoryClass : NSObject-(void)methodOne;@end#import "RuntimeCategoryClass.h"@implementation RuntimeCategoryClass-(void)methodOne{NSLog(@"methodOne!");
}@end
#import "RuntimeCategoryClass.h"@interface RuntimeCategoryClass (Category)-(void)methodTwo;@end#import "RuntimeCategoryClass+Category.h"@implementation RuntimeCategoryClass (Category)-(void)methodTwo{NSLog(@"methodTwo!");
}
@end
/*** 获取方法列表*/
-(void)testOne{unsigned int outCount = 0;Method *methodList = class_copyMethodList(RuntimeCategoryClass.class, &outCount);for (int i = 0; i const char *name = sel_getName(method_getName(method));NSLog(@"RuntimeCategoryClass's method: %s", name);if (strcmp(name, sel_getName(@selector(methodTwo))) ==0 ) {NSLog(@"分类方法methodTwo在objc_class的方法列表中");}}}
推荐:
http://tech.meituan.com/DiveIntoCategory.html