2017-05-08 85 views
0
+ (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'

卻不知道爲什麼.....

回答

0

爲iPad。 我運行'p class_getInstanceMethod(aClass,@selector(setBackgroundColor :))' 控制檯無法輸出函數地址。 但iPhone。我可以得到地址。

所以我嘗試 'P class_getInstanceMethod(ACLASS,@selector(_setBackgroundColor))'

問題解決了..

但它只是奇怪。如果你知道其他的解決辦法。 請讓我知道。

0

與我一樣的崩潰,我解決它使用UIView而不是UITableView。