2014-02-06 26 views
0

我需要使用http请求的结果执行重复任务,因此想要将完成hander的声明存储在变量中(或者以某种方式将其声明为函数可以通过)。变量iOS中的存储完成处理程序(目标c)

示例代码:

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    // carry out some action 
}]; 

我想能够写

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler: standardHandler ]; 

standardHandler含有嵌段功能。

这可能吗?

我对Objective C非常陌生,所以很抱歉,如果这是一个明显的问题。

回答

5

肯定的:

void (^completionHandler)(NSURLResponse *, NSData *, NSError *) = ^(NSURLResponse *response, NSData *data, NSError *error) { 
    // carry out some action 
}; 
[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:completionHandler]; 
+1

我喜欢的网站的网址! :)感谢您的信息,我会尽快接受答案! –