标签:develop 就会 abi exchange 签名 cmd 对象 信息 cache
Objective-C Runtime
Note
char *
in the runtime API should be considered to have UTF-8 encoding.1 struct objc_class { 2 Class isa OBJC_ISA_AVAILABILITY; 3 #if !__OBJC2__ 4 Class super_class OBJC2_UNAVAILABLE; 5 const char *name OBJC2_UNAVAILABLE; 6 long version OBJC2_UNAVAILABLE; 7 long info OBJC2_UNAVAILABLE; 8 long instance_size OBJC2_UNAVAILABLE; 9 struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; 10 struct objc_method_list **methodLists OBJC2_UNAVAILABLE; 11 struct objc_cache *cache OBJC2_UNAVAILABLE; 12 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; 13 #endif 14 } OBJC2_UNAVAILABLE;
id objc_msgSend(id self, SEL _cmd, …);
1 /** 2 要动态绑定的方法 3 4 @param self 要绑定方法的对象 5 @param _cmd 方法信息 6 */ 7 void dynamicMethodIMP(id self, SEL _cmd) { 8 9 NSLog(@"SEL: %s method is added", sel_getName(_cmd)); 10 NSLog(@"Name: Jackey"); 11 } 12 13 14 /** 15 动态绑定和解析方法 16 17 @param sel 方法信息 18 @return 是否已经处理该方法 19 */ 20 + (BOOL)resolveInstanceMethod:(SEL)sel { 21 22 NSLog(@"SEL: %s method does not exist", sel_getName(sel)); 23 24 if (sel == @selector(printName)) { 25 26 class_addMethod ([self class], sel, (IMP) dynamicMethodIMP, "v@:"); 27 return YES; 28 } 29 30 return [super resolveInstanceMethod:sel]; 31 }
这样我们再运行[[ClassA new] printName];就会输出Name: Jackey
重定向:
如果经过动态解析后, 消息还没有被响应就会进入到重定向阶段
我们可以重写- (id)forwardingTargetForSelector:(SEL)aSelector将消息重定向给可以响应的对象
1 /** 2 方法重定向 3 4 @param aSelector 方法信息 5 @return 返回重定向后要相应的对象 6 */ 7 - (id)forwardingTargetForSelector:(SEL)aSelector { 8 9 NSLog(@"Current class can‘t response to SEL: %s", sel_getName(aSelector)); 10 11 if (aSelector == @selector(printRightName)) { 12 13 NSLog(@"Forward to target: %@", [ClassB class]); 14 return [ClassB new]; 15 } 16 17 return [super forwardingTargetForSelector:aSelector]; 18 19 }
最后如果前面都没有处理就会进入到消息转发, 我们可以通过重写
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;
- (void)forwardInvocation:(NSInvocation *)anInvocation;
来自定义
1 /** 2 转发前, 获取方法签名 3 4 @param selector 方法信息 5 @return NSInvocation消息对象 6 */ 7 - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { 8 9 NSString *sel = NSStringFromSelector(selector); 10 if ([sel rangeOfString:@"set"].location == 0){ 11 12 return [NSMethodSignature signatureWithObjCTypes:"v@:@"]; 13 } 14 else{ 15 16 return [NSMethodSignature signatureWithObjCTypes:"@@:"]; 17 } 18 } 19 20 /** 21 转发 22 23 @param anInvocation 消息对象 24 */ 25 - (void)forwardInvocation:(NSInvocation *)anInvocation { 26 27 NSLog(@"No class can‘t response to SEL: %s", sel_getName([anInvocation selector])); 28 29 ClassC *c = [ClassC new]; 30 if ([c respondsToSelector:[anInvocation selector]]) { 31 32 NSLog(@"method apply deliver to %@", [ClassC class]); 33 [anInvocation invokeWithTarget:c]; 34 } 35 36 else { 37 38 [super forwardInvocation:anInvocation]; 39 } 40 }
消息的转发弥补了OC不能多继承的问题
最后我们来看下Method Swizzling
我们可以直接修改方法的指针, 让一个方法名指向其他的方法实现
1 Method ori_method = class_getInstanceMethod([ClassB class], @selector(printRightName)); 2 Method my_method = class_getInstanceMethod([ClassC class], @selector(printFamilyName)); 3 4 method_exchangeImplementations(ori_method, my_method); 5 6 [[ClassB new] printRightName];
使用method_exchangeImplementation交换了两个对象方法的指针
printRightName执行的实际是printFamilyName
标签:develop 就会 abi exchange 签名 cmd 对象 信息 cache
原文地址:http://www.cnblogs.com/zhouxihi/p/6107467.html