#import <Foundation/Foundation.h>
@interface Dog : NSObject
@end _______________________________________________
#import "Dog.h" #import <UIKit/UIKit.h> #import <objc/message.h>
@implementation Dog
//當(dāng)類方法或?qū)ο蠓椒]有實(shí)現(xiàn)就會調(diào)用這兩個方法中的一個 //處理類方法 //+(BOOL)resolveClassMethod:(SEL)sel //處理對象方法 //+(BOOL)resolveInstanceMethod:(SEL)sel
+(BOOL)resolveInstanceMethod:(SEL)sel {
NSLog(@"%@方法沒有實(shí)現(xiàn)",NSStringFromSelector(sel));if (sel == @selector(wangwangwang:)) {//動態(tài)添加方法/* cls: 類類型 name: 方法編號 imp: 方法實(shí)現(xiàn),函數(shù)指針 types: 函數(shù)類型 C字符串(Code)void === "v" */class_addMethod([Dog class], sel, (IMP)wangwangwang, "v@:@");} else if( sel ==@selector(wangwangwang1) ){ //參數(shù)意義具體參考官方文檔 class_addMethod([Dog class], sel, (IMP)wangwangwang1, "v@:");}return [super resolveClassMethod:sel];}
void wangwangwang(id self, SEL _cmd,id obj) {
NSLog(@"帶參數(shù)%@",obj);}
void wangwangwang1(id self, SEL _cmd) {
NSLog(@"不帶參數(shù)");}
@end _______________________________________________ 調(diào)用 #import "HJViewController.h" #import "Dog.h" @interface HJViewController ()
@end
@implementation HJViewController
- (void)viewDidLoad { [super viewDidLoad];
}
@end
新聞熱點(diǎn)
疑難解答