【问题标题】:Xcode8+ ipad. Method Swizzling UITableView backgroundColor crashXcode8+ ipad。方法 Swizzling UITableView backgroundColor crash
【发布时间】:2017-10-06 07:43:05
【问题描述】:
+ (void)load {
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class aClass = [self class];
        SEL selector = @selector(setBackgroundColor:);
        SEL _selector = @selector(cusSetBackgroundColor:);

        Method method = class_getInstanceMethod(aClass, selector);
        Method _method = class_getInstanceMethod(aClass, _selector);

        BOOL did = class_addMethod(aClass, selector, method_getImplementation(_method), method_getTypeEncoding(_method));
        if (did) {
            class_replaceMethod(aClass, _selector, method_getImplementation(method), method_getTypeEncoding(method));
        } else {
            class_addMethod(aClass, _selector, method_getImplementation(_method), method_getTypeEncoding(_method));
            method_exchangeImplementations(method, _method);
        }
    });
}
- (void)cusSetBackgroundColor:(UIColor *)backgroundColor
{
    NSLog(@"test swizzling");
    [self cusSetBackgroundColor:backgroundColor];
}

当我将它放入“UITableView+Swizzling”时。并选择 iPad Pro。它会崩溃。 (iPhone 运行良好)

崩溃日志是: void PushNextClassForSettingIMP(id, SEL()) 中的断言失败。 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序

不知道为什么.....

【问题讨论】:

    标签: ios objective-c uitableview ipad swizzling


    【解决方案1】:

    适用于 iPad。 我运行'p class_getInstanceMethod(aClass,@selector(setBackgroundColor :))' 控制台无法输出函数地址。 但对于 iPhone。我可以得到地址。

    所以我尝试了 'p class_getInstanceMethod(aClass, @selector(_setBackgroundColor))'

    问题解决了..

    但这太奇怪了。如果你知道其他解决方案。 请告诉我。

    【讨论】:

      【解决方案2】:

      我遇到了同样的崩溃,我使用 UIView 而不是 UITableView 解决了它。

      【讨论】:

        猜你喜欢
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多