Swizzling和超级 [英] Swizzling and super

查看:82
本文介绍了Swizzling和超级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在通过存储原始的实现来调用UIResponder及其所有子类的canPerformAction:withSender:方法。在由类名称键入的字典中;



这似乎在某些情况下运行良好,但是在原始实现时失败了调用超级。



这里可能出现什么错误?

-original 与 -custom : p>

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

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

说的是,如果你有方法swizzled在superclass,objc_msgSendSuper会做同样的事情:调用原来的自定义,反之亦然,给你递归。






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

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


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?

解决方案

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
}

这篇关于Swizzling和超级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆