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 *) ACO;
-(Void) myEncodeWithCoder :( NSCoder *) ACO;
@ 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 *) ACO {
// TODO:
}
-(Void) myEncodeWithCoder :( NSCoder *) ACO {
// TODO:
}
@ End