Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.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 AFN网络中的返回响应_Ios_Afnetworking - Fatal编程技术网

Ios AFN网络中的返回响应

Ios AFN网络中的返回响应,ios,afnetworking,Ios,Afnetworking,我按照教程学习IOS中的网络 我使用以下函数从服务器获取响应: { // 1 NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString]; NSURL *url = [NSURL URLWithString:weatherUrl]; NSURLRequest *request = [NSURLRequest requestWithURL

我按照教程学习IOS中的网络 我使用以下函数从服务器获取响应:

{
    // 1
    NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *url = [NSURL URLWithString:weatherUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2
    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        // 3
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            //Success
        }
        // 4
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                         message:[NSString stringWithFormat:@"%@",error]
                                                        delegate:nil
                                               cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [av show];
        }];

    // 5
    [operation start];
}
我想要的是编写一个函数,在得到响应后,它将以
NSString
的形式返回响应。我不懂语法,有人能帮我吗

Try this


- (void)getResponse:(void (^)(id result, NSError *error))block {
       NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
       NSURL *url = [NSURL URLWithString:weatherUrl];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];

      // 2
      AFJSONRequestOperation *operation =
       [AFJSONRequestOperation JSONRequestOperationWithRequest:request
      // 3
      success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        //Success
                   block(JSON,nil); //call block here
      }
     // 4
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)    {
          UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving     Weather"
                                                     message:[NSString stringWithFormat:@"%@",error]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK" otherButtonTitles:nil];
           [av show];
      }];

  // 5
  [operation start];

}
召唤

[self getResponse:^(id result, NSError *error) {
         //use result here
 }];

希望这有帮助

您可以像这样记录//成功所在的位置

NSLog(@"%@", JSON);
或者,如果您需要字符串格式,则:

[NSString stringWithFormat:@"JSON response is %@", JSON];

希望这有帮助。

因为您的示例是异步的,所以该方法将在收到响应之前返回。只需在success块中调用一个方法(在该块中编写注释
//success
),并将收到的JSON传递给该方法。感谢您的回复。当我试图编译您的cod时,我在块中得到了错误(JSON,nil);“ARC不允许将Objective-C指针隐式转换为“\u autoreleasing id*”嘿,我刚刚删除了*in*result。然后它就工作了。谢谢大家,所以我们需要删除*before result in参数,它会工作的