在Swift中使用AFNetworking接收响应 [英] Receiving response using AFNetworking in Swift

查看:231
本文介绍了在Swift中使用AFNetworking接收响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AFNetworking的新手,并且我尝试在同一位置遵循Raywenderlich的教程.因此,我已经获得了用于JSON解析的代码,并且正在努力将其转换为Swift.我已经尝试了许多教程和StackOverflow答案,但是找不到一个很好的说明.

I am new to AFNetworking and I am trying to follow the tutorial of Raywenderlich on the same. So I have got this code for JSON parsing and I am struggling with converting that to Swift. I have tried many tutorials and StackOverflow answers but couldn't find a good explanatory one.

- (IBAction)jsonTapped:(id)sender
{
    // 1
    NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

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

        // 3
        self.weather = (NSDictionary *)responseObject;
        self.title = @"JSON Retrieved";
        [self.tableView reloadData];

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

        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    // 5
    [operation start];
}

那么,关于使用AFNetworking解析数据的基础知识,有人可以帮我吗?

So, can any one please help me regarding the basics of parsing data with AFNetworking?

推荐答案

问题是为什么要在Swift中使用Objc. AFNetworking有很多替代框架.

The problem is why you are going with Objc in Swift. There are many frameworks alternative to AFNetworking.

实际上,同一位开发人员已经在Swift中开发了名为 Alamofire的网络库.

In fact, the same developer has developed the Network Library in Swift named Alamofire

您可以使用该框架并获得相同的响应:

You can use that framework and get the same response:

这里是演示示例!

func postWebserviceWithURL(strUrl: String, param: NSDictionary?, completionHandler: (NSDictionary?, NSError?) -> ()) -> (){

        Alamofire.request(.POST, strUrl, parameters: param as? [String : AnyObject], encoding: ParameterEncoding.URL).responseJSON { response in

            switch response.result {

            case .Success(let data):
                let json = data as? NSDictionary
                completionHandler(json, nil)
            case .Failure(let error):
                completionHandler(nil, error)
                self.showSingleAlert("app_name", message: error.localizedDescription)
            }
        }
    }

这篇关于在Swift中使用AFNetworking接收响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆