Search code examples
iosmethodsparametersreusability

iOS method reuse (pass parameters)


I have an url request method, because each page will be used, I want to make it public. (Poor standard of English, forgive me) here is my class method:

- (void)httpRequest :(NSString *)url :(NSMutableArray *)array;

{

    NSURL *myUrl = [NSURL URLWithString:url];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:myUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)

     {

         NSLog(@"请求完成");

         NSArray *arr;

         arr = [NSJSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingAllowFragments error:NULL];

         [array addObjectsFromArray:arr];

            ???????

     }failure:^(AFHTTPRequestOperation *operation, NSError *error){

         NSLog(@"请求失败: %@", error);
            ??????
     }];
    [operation start];
}

I know how to pass simple parameters,like array and url.But when I use this method,code in the question mark will be different everytime.What can I do to make it can be passed,like parameters.

Like: [myApple httpRequest :myurl :myarray : (susucess .....) : (failure......)]; the question mark can be filled in: mytableView reloadData or nslog...
I'm not sure if i explained my question clearly,i don't know if block can solve this,thanks,waiting for your help.


Solution

  • Firstly, note that all method parameters in Objective-C should be named. So instead of - (void)httpRequest :(NSString *)url :(NSMutableArray *)array;, you should have something like:

    - (void)performHttpRequestWithURL:(NSString *)url resultsArray:(NSMutableArray *)array; 
    

    To answer your question, you could pass your own completion block into your method. You can then call this block once AFNetworking has completed its HTTP request.

    - (void)performHTTPRequestWithURL:(NSString *)urlString completion:(void (^)(NSArray *results, NSError *error))completion
    {
        NSURL *url = [NSURL URLWithString:urlString];
    
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                  timeoutInterval:10];
    
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
        {
            NSLog(@"请求完成");
    
            NSArray *results = [NSJSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingAllowFragments error:nil];
    
            if (completion)
                completion(results, nil);
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error)
        {
            NSLog(@"请求失败: %@", error);
    
            if (completion)
                completion(nil, error);
        }];
    
        [operation start];
    }
    

    You could then call your method and pass in a block:

    [self performHTTPRequestWithURL:@"http://www.example.com" completion:^(NSArray *results, NSError *error) {
        if (error) 
        {
              // handle error
        }
        else
        {
            // reload tableview or similar
        }
     }];
    

    Take a look at Apple's Blocks Programming Guide for more information on blocks.