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

关于dealloc swizzleInstanceMethod处理优化建议 #2

Open
gonghongxia opened this issue May 11, 2017 · 1 comment
Open

关于dealloc swizzleInstanceMethod处理优化建议 #2

gonghongxia opened this issue May 11, 2017 · 1 comment

Comments

@gonghongxia
Copy link

因KVO防护需求,需要在dealloc中做一些处理,所以重定向了dealloc。但会引发原有dealloc方法的未处理,所以[self swizzleInstanceMethodWithOriginSel:NSSelectorFromString(@"dealloc") swizzledSel:@selector(baymax_dealloc)];应改为替换并添加原有dealloc消息转发。

@ParsifalC
Copy link
Owner

@gonghongxia 额,不是很明白你说的是哪个情况。原有dealloc方法为什么会未处理呢?不过关于dealloc方法的处理是可以单独拎出来写的,当时只是为了尝试下方案的可行性,零零散散地写得比较随便。另外,测试用例也可以优化下,现在这样有点low。哈哈。可以提供个思路给你,有空的话可以直接写完提PR。测试用例这块,可以分为不同的tester写,然后通过runtime去遍历tester的方法,最终效果大概是首页是不同类型的tester,点击tester后,到另一个页面,是具体的每个Test Case。大致是这样子的:

+ (instancetype)tester:(WYCrashDoctorType)type {
    switch (type) {
        case WYCrashDoctorUnrecognizedSelector:
            return [WYUnrecognizedSelectorTester new];
        case WYCrashDoctorCollection:
            return [WYCollectionTester new];
        case WYCrashDoctorKVO:
            return [WYKVOTester new];
        case WYCrashDoctorTimer:
            return [WYTimerTester new];
        case WYCrashDoctorString: {
            return [WYStringTester new];
        case WYCrashDoctorNotification:
            return [WYNotificationTester new];
        case WYCrashDoctorUI:
            return [WYUITester new];
        case WYCrashDoctorBadAccess:
            return [WYBadAccessTester new];
        case WYCrashDoctorBooting:
            return [WYBootingTester new];
        }
            
        default:
            return [self new];
    }
}
- (NSArray *)testCaseSelectors {
    NSMutableArray *seletors = @[].mutableCopy;
    unsigned int outCount;
    Method *methodList = class_copyMethodList([self class], &outCount);
    
    for (unsigned int i = 0; i < outCount; i++) {
        Method method = methodList[i];
        NSString *selStr = NSStringFromSelector(method_getName(method));
        
        if ([selStr hasPrefix:@"test_"]) {
            [seletors addObject:selStr];
        }
    }
    
    free(methodList);
    return seletors;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants