【发布时间】:2014-07-18 17:46:37
【问题描述】:
我使用 NSProxy 模拟一个类,并希望挂钩该类的所有调用。但是只有类外调用的方法才被钩住,类内没有调用的方法。下面是我的代码:
在我的 AppDelegate.m 中,TBClassMock 是 NSProxy
的子类TBClassMock *mock = [[TBClassMock alloc] init];
TBTestClass *foo = [[TBTestClass alloc] init];
mock.target = foo;
foo = mock;
[foo outsideCalled];
在我的 TBTestClass.m
- (void)outsideCalled
{
[self insideCalled];
}
- (void)insideCalled
{
}
在我的 TBClassMock.m
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSLog(@"Signature: %@", NSStringFromSelector(selector));
return [self.target methodSignatureForSelector:selector];
}
-(void)forwardInvocation:(NSInvocation*)anInvocation
{
//... Do other things
[anInvocation invokeWithTarget:self.target];
}
然后我可以记录[foo outsideCalled]的调用,但不能记录[self insideCalled]的调用。
我的目标是在//... Do other things 中的类的所有调用中做一些事情,这种方式似乎失败了。关于这个和任何其他方法来实现我的要求的任何解释?我只是不想使用method_exchangeImplementations 来调整类的所有方法,因为我认为这太挑剔而且不是一个好方法。
【问题讨论】:
-
TBTestClass.h中声明了哪些方法?