0

不需要以前的 AFNetworking 知识,但它可能会有所帮助。

我正在使用 AFNetworking,我想创建一个 AFHTTPRequestOperation 的子类,它有几个额外的属性和方法。

我遇到的问题是我正在使用 AFHTTPRequestOperationManager 的工厂方法来生成我的 AFHTTPRequestOperations 并且我显然不想更改 AFNetworking 库中的任何核心代码。

所以我想做的是这样的:

@implementation VP_AFHTTPRequestOperation : AFHTTPRequestOperation

+ (id)instantiateUsingAFHTTPRequestOperation:(AFHTTPRequestOperation*)reqOp {
    //I want to create an instance of VP_AFHTTPRequestOperation using the reqOp
    self = reqOp; //I can't just do this, but I don't know what to do
    return self;
}

- (void)mySubclassMethod { /* ... */ }
@end

我会很感激任何建议

4

2 回答 2

0

子类 AFHTTPRequestOperationManager 并让它返回一些好的东西HTTPRequestOperationWithRequest

无需修改任何框架代码 AFAICS

@interface VP_AFHTTPRequestOperationManager : AFHTTPRequestOperationManager 
@end     

@implementation VP_AFHTTPRequestOperationManager

+ (instancetype)manager { 
    return [[[self class] alloc] init]; 
}

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
                                                success:(void (^) (AFHTTPRequestOperation *operation, id responseObject))success
                                                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    VP_AFHTTPRequestOperation *operation = [[VP_AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = self.responseSerializer;
    operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
    operation.credential = self.credential;
    operation.securityPolicy = self.securityPolicy;

    [operation setCompletionBlockWithSuccess:success failure:failure];

    return operation;
}

@end
于 2013-10-11T18:30:30.527 回答
0

我认为你可以通过子类化和在覆盖中AFHTTPRequestOperationManager使用你的子类来做你想做的事。文档中简洁地描述了它:AFHTTPRequestOperationHTTPRequestOperationWithRequest:success:failure

要更改 AFHTTPRequestOperationManager 子类的所有请求操作构造的行为,请覆盖 HTTPRequestOperationWithRequest:success:failure。

于 2013-10-11T18:48:11.967 回答