@interface ErrorObject : NSObject - (int)sum:(int)a with:(int)b; - (int)returnSelf:(int)a; @end @implementation ErrorObject - (int)returnSelf:(int)a { return a; } - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { if (aSelector == @selector(sum:with:)) { NSMethodSignature *method = [NSMethodSignature signatureWithObjCTypes:"i:i"]; return method; } return [super methodSignatureForSelector:aSelector]; } - (void)forwardInvocation:(NSInvocation *)anInvocation { anInvocation.selector = @selector(returnSelf:); [anInvocation invokeWithTarget:self]; } @end
https://www.jianshu.com/p/fdd8f5225f0c
1.动态添加对应方法选择器的方法实现(参数类型和数量不可变)
2.返回一个备用接收者(参数类型和数量不可变,由备用者来处理这个消息)
3.返回一个新的方法签名(可以设置新的参数数量 -- 从原消息中选取或添加参数,后续使用NSInvocation来消化这个消息)
4.以上方法均未成功,抛出异常