可变的NSHTTPURLResponse或NSURLResponse [英] Mutable NSHTTPURLResponse or NSURLResponse
问题描述
我需要在NSURLResponse中修改响应头.这可能吗?
I need to modify response headers in an NSURLResponse. Is this possible?
推荐答案
我只是和一个朋友谈论这件事.我的建议是编写NSURLResponse的子类.遵循以下原则:
I was just talking about this with a friend. My suggestion would be to write a subclass of NSURLResponse. Something along these lines:
@interface MyHTTPURLResponse : NSURLResponse { NSDictionary *myDict; }
- (void)setAllHeaderFields:(NSDictionary *)dictionary;
@end
@implementation MyHTTPURLResponse
- (NSDictionary *)allHeaderFields { return myDict ?: [super allHeaderFields]; }
- (void)setAllHeaderFields:(NSDictionary *)dict { if (myDict != dict) { [myDict release]; myDict = [dict retain]; } }
@end
如果您要处理的对象不是您要制作的,则可以尝试使用object_setClass
清除类.但是我不知道这是否会添加必要的实例变量.如果可以支持足够新的SDK,也可以使用objc_setAssociatedObject
并将其全部塞入类别中.
If you're dealing with an object you didn't make, you can try using object_setClass
to swizzle the class out. However I don't know if that will add the necessary instance variable. You could also use objc_setAssociatedObject
and stuff this all in a category instead, if you can support a new enough SDK.
这篇关于可变的NSHTTPURLResponse或NSURLResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!