Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Objective c 如何使用sendAsynchronousRequest之外的值?_Objective C_Json_Sendasynchronousrequest - Fatal编程技术网

Objective c 如何使用sendAsynchronousRequest之外的值?

Objective c 如何使用sendAsynchronousRequest之外的值?,objective-c,json,sendasynchronousrequest,Objective C,Json,Sendasynchronousrequest,我正在使用HTTPPOST请求和NSURLRequest解析一些JSON数据。但是,当我在sendAsynchronousRequest下获得了这些值时,我不能在该请求的外部使用这些值。请参见下面的示例: [NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

我正在使用HTTPPOST请求和NSURLRequest解析一些JSON数据。但是,当我在sendAsynchronousRequest下获得了这些值时,我不能在该请求的外部使用这些值。请参见下面的示例:

[NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSError *parseError = nil;
         dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
         NSLog(@"Server Response (we want to see a 200 return code) %@",response);
         NSLog(@"dictionary %@",dictionary);
     }];

我的问题是如何在需要字典值的地方使用它?谢谢

你可以用很多方法来做。一种方法是声明属性并在块内使用它

在进行异步调用时,最好有自己的自定义块来响应这些调用

首先声明完成块:

 typedef void (^ ResponseBlock)(BOOL success, id response);
并声明将此块用作参数的方法:

 - (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion;
并在此方法中包含异步调用:

- (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion{

 [NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSError *parseError = nil;
     dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
     NSLog(@"Server Response (we want to see a 200 return code) %@",response);
     NSLog(@"dictionary %@",dictionary);
     completion(YES,response); //Once the async call is finished, send the response through the completion block
 }];

}
您可以在任何地方调用此方法

 [classInWhichMethodDeclared processMyAsynRequestWithCompletion:^(BOOL success, id response) {
      //you will receive the async call response here once it is finished.
         NSDictionary *dic = (NSDictionary *)response;
       //you can also use the property declared here
           _dic = (NSDictionary *)response; //Here dic must be declared strong
 }];

你可以用很多方法来做。一种方法是声明属性并在块内使用它

在进行异步调用时,最好有自己的自定义块来响应这些调用

首先声明完成块:

 typedef void (^ ResponseBlock)(BOOL success, id response);
并声明将此块用作参数的方法:

 - (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion;
并在此方法中包含异步调用:

- (void)processMyAsynRequestWithCompletion:(ResponseBlock)completion{

 [NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSError *parseError = nil;
     dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
     NSLog(@"Server Response (we want to see a 200 return code) %@",response);
     NSLog(@"dictionary %@",dictionary);
     completion(YES,response); //Once the async call is finished, send the response through the completion block
 }];

}
您可以在任何地方调用此方法

 [classInWhichMethodDeclared processMyAsynRequestWithCompletion:^(BOOL success, id response) {
      //you will receive the async call response here once it is finished.
         NSDictionary *dic = (NSDictionary *)response;
       //you can also use the property declared here
           _dic = (NSDictionary *)response; //Here dic must be declared strong
 }];

你可以用它inside@vadian,我想将这些值放在表视图中。那么我如何在内部使用它呢?将填充和重新加载表视图的代码放在完成块中。异步思考您可以使用它inside@vadian,我想将这些值放在表视图中。那么我如何在内部使用它呢?将填充和重新加载表视图的代码放在完成块中。异步考虑在ClassInWhichMethodedClared中不获取字典值。ClassInWhichMethodedClared是您声明processMyAsynRequestWithCompletion方法的地方。Teja,您的代码工作得很好。谢谢但是我想把我的值放在一个表视图中。未获取TableView上的值。在processMyAsynRequestWithCompletion中,调用TableView的reloadData,在datasource中,方法使用_dic,因为它包含下载的数据。实际上,当我在ViewDid上调用该方法时,加载其获取的数据,但如何全局使用这些数据?在ClassinWhichMethodedClared中,不获取字典值。ClassinWhichMethodClared是您声明processMyAsynRequestWithCompletion方法的地方。Teja,你的代码工作得很好。谢谢但是我想把我的值放在一个表视图中。未获取TableView上的值。在processMyAsynRequestWithCompletion中,调用TableView的reloadData,在datasource方法中使用_dic,因为它包含下载的数据。实际上,当我在Views上调用该方法时,加载了它的获取数据,但如何全局使用这些数据?