不要使用类别来覆盖方法。根据文档
"If the name of a method declared in a category is the same as a
method in the original class, or a method in another category on
the same class (or even a superclass), the behavior is undefined
as to which method implementation is used at runtime. "
请参阅“避免类别方法名称冲突”中的文档->
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf
子类化并覆盖该方法并使用子类?
分析您的场景,进行方法调配是有意义的。注意:确保 yourCache 的行为方式与 sharedImageCache 相同,否则会导致崩溃。
@implementation UIImageView (Swizzling)
+ (void)load {
static dispatch_once_t token;
dispatch_once(&token, ^{
Class myClass = [self class];
Method originalMethod = class_getInstanceMethod(myClass, @selector(sharedImageCache));
Method newMethod = class_getInstanceMethod(myClass, @selector(NewSharedImageCache:));
method_exchangeImplementations(originalMethod, newMethod);
});
}
//This is just for sample. you can create your buffer in your own way
static id <yourCache> yourBuffer;
+ (id <yourCache>)NewSharedImageCache
{
return yourBuffer;
}
@end