一.消息转发机制
void sendMessage(idself,SEL_cmd,NSString* msg){
NSLog(@"1===hello");
}
1.动态方法解析(resolveInstanceMethod)
+ (BOOL)resolveInstanceMethod:(SEL)sel{
if([NSStringFromSelector(sel) isEqualToString:@"sendMessage:"]){
returnclass_addMethod(self, sel, (IMP)sendMessage,"v@:@");
}
return NO;
}
2.快速转发(forwardingTargetForSelector)
- (id)forwardingTargetForSelector:(SEL)aSelector{
NSString* methodName =NSStringFromSelector(aSelector);
if([methodNameisEqualToString:@"sendMessage:"]){
return[MessageObjectnew];
}
return [super forwardingTargetForSelector:aSelector];
}
3.慢速转发
方法1.methodSignnatureForSeletor
方法2.forwardinvocation
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
NSString*methodName =NSStringFromSelector(aSelector);
if([methodNameisEqualToString:@"sendMessage:"]){
return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation*)anInvocation{
SELsel = [anInvocationselector];
MessageObject *object = [MessageObject new];
if([objectrespondsToSelector:sel]){
[anInvocationinvokeWithTarget:object];
}else{
[superforwardInvocation:anInvocation];
}
}
4.方法没实现调用
doesNotRecognizeSelector
- (void)doesNotRecognizeSelector:(SEL)aSelector{
NSLog(@"方法未实现");
}