当对象接收到不能处理的消息时调用的方法,下面三个方法会按顺序调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
// 但是自己测试当对象接收到不能处理的消息时调用的方法时,会调用这个方法,在这个方法中拿到sel
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
return signature;
}
// 这个方法也会调用
// 将消息转出某对象(让这个被转出的对象接受消息)
- (id)forwardingTargetForSelector:(SEL)aSelector {
NSLog(@"MyTestObject _cmd: %@", NSStringFromSelector(_cmd));
FooClass *foo = [[FooClass alloc] init];
if ([foo respondsToSelector: aSelector]) {
return none;
}
return [super forwardingTargetForSelector: aSelector];
}
// 当对象接收到不能处理的消息时会将这个sel传到这个函数中在这个方法中添加在自己.m文件中的函数实现
+ (BOOL)resolveInstanceMethod:(SEL)aSEL {
if (aSEL == @selector(resolveThisMethodDynamically)) {
class_addMethod([self class], aSEL, (IMP)dynamicMethodIMP, "v@:");
return YES;
}
return [super resolveInstanceMethod:aSEL];
}
|
消息转发
1
2
3
4
5
6
7
8
|
/ / 用于消息转发
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.someOtherObject respondsToSelector:anInvocation.selector])
[anInvocation invokeWithTarget:self.someOtherObject];
else
[super forwardInvocation:anInvocation];
}
|