给类扩展方法
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface RuntimeCategoryClass:NSObject
- (void)method1;
@end
@implementation RuntimeCategoryClass
- (void)method1{
}
@end
@interface RuntimeCategoryClass (Categroy)
- (void)method2;
@end
@implementation RuntimeCategoryClass(Categroy)
- (void)method2{
}
@end
void test(){
#pragma mark---
NSLog(@"测试objc_class中的方法列表是否包含分类中得到方法");
uint outcount = 0;
Class cls = RuntimeCategoryClass.class;
Method *methodList =class_copyMethodList(cls, &outcount);
for (int i = 0 ; i<outcount; i++) {
Method method = methodList[i];
const char *name = sel_getName(method_getName(method));
NSLog(@"RuntimeCategoryClass's method:%s",name);
if (strcmp(name , sel_getName(@selector(method2)))) {
NSLog(@"分类中的方法method2在objc_class的方法列表中");
}
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
test();
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
网友评论