Xcode8 + ipad。方法Swizzling UITableView backgroundColor崩溃
问题描述:
+ (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运行良好)Xcode8 + ipad。方法Swizzling UITableView backgroundColor崩溃
崩溃日志为: void PushNextClassForSettingIMP(id,SEL())中的声明失败。 终止应用程序由于未捕获的异常 'NSInternalInconsistencyException'
却不知道为什么.....
答
为iPad。 我运行'p class_getInstanceMethod(aClass,@selector(setBackgroundColor :))' 控制台无法输出函数地址。 但iPhone。我可以得到地址。
所以我尝试 'P class_getInstanceMethod(ACLASS,@selector(_setBackgroundColor))'
问题解决了..
但它只是奇怪。如果你知道其他的解决办法。 请让我知道。
答
与我一样的崩溃,我解决它使用UIView而不是UITableView。