Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 子类化AFHTTPRequestOperation_Ios_Objective C_Afnetworking_Hierarchy_Afnetworking 2 - Fatal编程技术网

Ios 子类化AFHTTPRequestOperation

Ios 子类化AFHTTPRequestOperation,ios,objective-c,afnetworking,hierarchy,afnetworking-2,Ios,Objective C,Afnetworking,Hierarchy,Afnetworking 2,我正在使用AFNetworking 2.0库作为基础的网络层上工作。我计划为每个请求创建单独的类,并使用AFHTTPRequestOperation作为基类。 所以我有点像: ABCLoginOperation : AFHTTPRequestOperation 每个自定义包装器类都有一些附加属性,便于使用请求/响应参数。 因此,在我的案例中,工作流如下所示: 创建ABCLoginOperation类实例 通过设置其附加属性来配置ABCLoginOperation 运行ABCLoginOpera

我正在使用
AFNetworking 2.0
库作为基础的网络层上工作。我计划为每个请求创建单独的类,并使用
AFHTTPRequestOperation
作为基类。 所以我有点像:

ABCLoginOperation : AFHTTPRequestOperation
每个自定义包装器类都有一些附加属性,便于使用请求/响应参数。 因此,在我的案例中,工作流如下所示:

  • 创建
    ABCLoginOperation
    类实例
  • 通过设置其附加属性来配置
    ABCLoginOperation
  • 运行
    ABCLoginOperation
    ,将其添加到任何
    NSOperationQueue
  • 我要实现的目标是这样的:

    ABCLoginOperation *loginOperation = [[ABCLoginOperation alloc] init];
    ABCLoginRequestModel *requestModel = operation.requestModel;
    requestModel.username = @"username";
    requestModel.password = @"password";
    [ABCOperationManager enqueueOperation:operation];
    
    - (void)enqueueOperation:(ABCOperation *)operation {
        [self configureOperation:operation];
        [self.operationQueue addOperation:operation]; //or use AFHTTPOperationManager's operation queue
    }
    
    - (void)configureOperation:(ABCOperation *)operation {
        NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[operation getRequestPath]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:operation.timeoutInterval];
        //some further configuration of mutableUrlRequest    
        operation.request = mutableURLRequest //the problem
    }
    
    问题是,
    AFHTTPRequestOperation
    不知道我用来描述请求参数的其他属性,我必须在步骤2和步骤3之间解决这个问题

    因此,我实现了一个定制的请求管理器
    abcopeOptionManager
    ,它应该基于
    ABCLoginOperation的
    属性设置
    AFHTTPRequest的
    属性。我认为可以创建
    NSURLRequestObject
    ,根据
    ABCLoginOperation
    对象的属性设置其参数,并将该
    NSURLRequestObject
    设置为
    AFHTTPRequestOperation
    /
    AFURLConnectionOperation
    在其实现中使用的
    request
    属性。因此,
    abCopeOptionManager的
    排队操作:
    方法的实现如下:

    ABCLoginOperation *loginOperation = [[ABCLoginOperation alloc] init];
    ABCLoginRequestModel *requestModel = operation.requestModel;
    requestModel.username = @"username";
    requestModel.password = @"password";
    [ABCOperationManager enqueueOperation:operation];
    
    - (void)enqueueOperation:(ABCOperation *)operation {
        [self configureOperation:operation];
        [self.operationQueue addOperation:operation]; //or use AFHTTPOperationManager's operation queue
    }
    
    - (void)configureOperation:(ABCOperation *)operation {
        NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[operation getRequestPath]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:operation.timeoutInterval];
        //some further configuration of mutableUrlRequest    
        operation.request = mutableURLRequest //the problem
    }
    
    但问题是
    AFHTTPRequestOperation
    request
    属性是只读的,只能在构造函数中设置。因此,似乎没有办法预先配置我的
    ABCOperation
    ,因为创建它的唯一方法是使用-
    initWithRequest:
    构造函数。这使得
    AFHTTPRequestOperation
    子类化在我看来毫无用处。请您解释一下我做错了什么,以及在
    AFNetworking 2.0
    周围创建自定义包装层的正确方法是什么

    提前谢谢

    请求操作是用请求初始化的,之后是不可变的。您可以在子类中将
    request
    属性重写为
    readwrite
    ,但我不建议这样做。