0
這兩種攪拌方式之間存在細微的差異。我只是想澄清,如果有什麼根本不同或它們之間在這兩種機制之間攪拌有什麼區別
假設我們是在UIView
第一種方式混寫viewDidLoad
(使用class_addMethod
)錯誤:
@implementation UIView (SwizzleFirstWay)
+ (void)load {
SEL originalSelector = @selector(viewDidLoad);
SEL swizzledSelector = @selector(swizzled_viewDidLoad);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
class_addMethod(self,
originalSelector,
class_getMethodImplementation(self, originalSelector),
method_getTypeEncoding(originalMethod));
// Adding the method
class_addMethod(self,
swizzledSelector,
class_getMethodImplementation(self, swizzledSelector),
method_getTypeEncoding(swizzledMethod));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
+ (void)swizzled_viewDidLoad {
// ... the swizzled implementation
// ...
// ...
[self swizzled_viewDidLoad]; // calling back to the original implementation
}
@end
方式二(不使用class_addMethod
) :
+ (void)load {
SEL originalSelector = @selector(viewDidLoad);
SEL swizzledSelector = @selector(swizzled_viewDidLoad);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
// NOT USING class_addMethod
method_exchangeImplementations(originalMethod, swizzledMethod);
}
+ (void)swizzled_viewDidLoad {
// ... the swizzled implementation
// ...
// ...
[self swizzled_viewDidLoad]; // calling back to the original implementation
}
@end
您的代碼工作?您爲選擇器swizzled_viewDidLoad調用class_getInstanceMethod,但該函數是Class Method,而不是實例方法。 –