Question

I was using method swizzling to wrap all method invocations in a class with some extra functionality. Specifically I was:

  • Checking if the required object for this method call was in the cache
  • If the cache had that object return it.
  • If not, dispatch to the original implementation, populate the cache and return that.

I wanted to see if using NSProxy gave more readable code, plus thought NSProxy might make it easy to redirect method calls with an unknown number of arguments.

So, the NSProxy version works fine except for one important catch. Any internal calls on my sub-class don't go via the proxy.

Here's how I create the stand-in interceptor:

- (id)init
{
    self = [super init];
    if (self)
    {
        TyphoonAssemblyProxy* proxy = [[TyphoonAssemblyProxy alloc] initWithAssembly:self];
        self = proxy; // 'self' should now be the proxy. . . but it seems not :(
    }
    return self;
}

Internal calls (ie calls to 'self' within the proxied class) don't get routed through proxy:

Now, in a sub-class of my proxied class, I have code want internal calls to go via the proxy. But they don't.

- (id)knight
{
    return [TyphoonDefinition withClass:[Knight class] properties:^(TyphoonDefinition* definition)
    {
        //THIS NEXT CALL TO [self defaultQuest] SHOULD GO THROUGH PROXY MACHINERY!
        [definition injectProperty:@selector(quest) withDefinition:[self defaultQuest]]; 
        [definition injectProperty:@selector(damselsRescued) withValueAsText:@"12"];
        [definition setScope:TyphoonScopeDefault];
    }];
}

Question:

What is an easy way to get these internal goes to go via the proxy?

Was it helpful?

Solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top