Before xcode 4.3 we can use this to encode and decode NSCachedURLResponse:
@implementation NSCachedURLResponse (NSCoder)
- (void)encodeWithCoder:(NSCoder *)coder {
// TODO:
}
- (id)initWithCoder:(NSCoder *)coder {
// TODO:
}
@end
But in xcode 4.3, it will cause a warng.
There are two methods to avoid the warning.
One:
ref, https://github.com/rs/SDURLCache/pull/29
Two:
@interface NSCachedURLResponse (ABC)
+ (void)swapMethods;
- (id)myInitWithCoder:(NSCoder *)aCoder;
- (void)myEncodeWithCoder:(NSCoder *)aCoder;
@end
#include <objc/objc.h>
#import <objc/runtime.h>
#import <objc/message.h>
void SwapMethods(Class cls, SEL originalSel, SEL newSel) {
Method originalMethod = class_getInstanceMethod(cls, originalSel);
Method newMethod = class_getInstanceMethod(cls, newSel);
method_exchangeImplementations(originalMethod, newMethod);
}
@implementation NSCachedURLResponse (ABC)
+ (void)swapMethods {
Class tempClass = [NSCachedURLResponse class];
SwapMethods(tempClass, @selector(initWithCoder:), @selector(myInitWithCoder:));
SwapMethods(tempClass, @selector(encodeWithCoder:), @selector(myEncodeWithCoder:));
}
- (id)myInitWithCoder:(NSCoder *)aCoder {
// TODO:
}
- (void)myEncodeWithCoder:(NSCoder *)aCoder {
// TODO:
}
@end
本文介绍了在Xcode4.3版本中避免NSCachedURLResponse编码警告的两种方法:使用第三方库或自定义编码解码方法。详细解释了如何通过替换或扩展NSCachedURLResponse类来实现。


被折叠的 条评论
为什么被折叠?



