Swizzling and super

落花浮王杯 提交于 2019-12-20 04:34:10

问题


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?


回答1:


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
}


来源:https://stackoverflow.com/questions/11504133/swizzling-and-super

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!