Karthik
Karthik

Reputation: 820

Swizzling and super

I am trying to swizzle the canPerformAction:withSender: method for UIResponder and all its subclasses which have overridden this method.

I am doing this by storing the original implementations in a dictionary keyed by class name; and looking up the dictionary in the swizzled version of the implementation before calling out to the original implementation.

This seems to work fine for some cases, but fails when the original implementation calls out to super. Then my swizzled method continuously keeps getting invoked and the program gets into infinite recursion.

What could be wrong here?

Upvotes: 2

Views: 1317

Answers (1)

A-Live
A-Live

Reputation: 8944

After the swizzle the -original with -custom:

-(void)custom {
    [self custom]; // calls -original
}

-(void)original {
    [self original]; // calls -custom
}

Said that, if you have the methods swizzled in the superclass, objc_msgSendSuper will do the same: call original for custom and versa giving you the recursion.


-(void)custom {
    [self original]; // calls -custom, makes recursion
}

 -(void)original {
    [self custom]; // calls -original, makes recursion
}

Upvotes: 2

Related Questions