Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift和OC混编,Timer防护Crash #123

Closed
LuckyCat7848 opened this issue Apr 18, 2022 · 3 comments
Closed

Swift和OC混编,Timer防护Crash #123

LuckyCat7848 opened this issue Apr 18, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@LuckyCat7848
Copy link

问题:崩溃在 NSTimer+CleanTimer.m line 58

原因:Unrecognized selector +[XXX.XXXLoadingView methodSignatureForSelector:]
原因解析:XXXXXLoadingView是一个Swift class类,没有继承于NSObject,所以没有methodSignatureForSelector方法。
主要代码如下:
class XXXLoadingView {
var unFreezeTimer = Timer()
static var shared = HFSLoginLoadingView()
private init(){}

func startAnimate() {
    self.unFreezeTimer = Timer.scheduledTimer(timeInterval: 5, target: XXXLoadingView.self, selector: #selector(XXXLoadingView.hideLoadingView), userInfo: nil, repeats: true)
}

@objc class func hideLoadingView() {
    let loadingView = HFSLoginLoadingView.shared
    loadingView.unFreezeTimer.invalidate()
}

}

解决办法:

  1. 根据原因解析,使XXXLoadingView继承于NSObject即可。但所有Swift使用Timer都非要为此继承NSObject实在不是个好方式;
  2. 希望作者在NSTimer+CleanTimer中处理,我尝试了以下代码是可以的。可能也不是最好的方式,希望以供参考,作者更新一版本。还有基于此,这些防护是全局性的,希望能顺便检查其他功能,完善的更好。使混编时也可以放心使用JJException,作者大大辛苦啦🌹

@implementation TimerObject

  • (void)fireTimer{
    ......
    if ([self.target respondsToSelector:self.selector]) {
    // 加了这一段
    if (![self.target respondsToSelector:@selector(methodSignatureForSelector:)]) {
    [self.target performSelector:self.selector withObject:_timer];
    return;
    }
    NSMethodSignature* signature = [self.target methodSignatureForSelector:self.selector];
    ......
    }
    }
    @EnD
@jezzmemo
Copy link
Owner

因为Swift的默认基类是SwiftObject,而SwiftObject的methodSignatureForSelector已经没有了,所以出现了这个问题,我看如何兼容下这个问题,谢谢您的建议

@jezzmemo jezzmemo added the bug Something isn't working label Apr 20, 2022
jezzmemo added a commit that referenced this issue Apr 21, 2022
@jezzmemo
Copy link
Owner

pod 'JJException',:git => 'https://github.com/jezzmemo/JJException.git'

麻烦帮忙确认下,观察几天再发版

@LuckyCat7848
Copy link
Author

作者每次解决都很快啊👍🏻!在项目中更新代码,查看已解决这个问题,也测试了Swift和OC各有参无参的case都OK。👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants