如何使用Swift进行网络请求呢?
我们首先创建一个Swift工程,工程名是SwiftDemo
然后在该工程中新建一个 Oc的类,那么Xcode会询问你是否创建 桥接 Oc和 Swift的 SwiftDemo-Bridging-Header.h文件
选择YES就会生成 工程名-Bridging-Header.h文件,在这个文件中我们引入Oc框架对应的头文件即可实现在Swift工程中调用Oc代码
目前的AFNetworking框架是Oc写的,我们要在Swift使用AF的话,则需要在 Bridging-Header.h文件中导入 "AFNetworking.h"即可
效果如下:
最简单的AF Get请求
- //
- // AFRequestTest.swift
- // SwiftDemo
- //
- // Created by MBinYang on 15/4/9.
- // Copyright (c) 2015年 cc.huanyouwang. All rights reserved.
- //
- import UIKit
- class AFRequestTest: NSObject {
- func afRequestTest()
- {
- var paramDict = ["access_token":"2.00HZE3aF0GvJSM551ca8e0920NF13N","uid":"5117873025"]
- var afManager = AFHTTPRequestOperationManager()
- var op = afManager.GET("http://lovemyqq.sinaapp.com/getState.PHP",parameters:paramDict,success: { (operation: AFHTTPRequestOperation!,responSEObject: AnyObject!) in
- var arr: AnyObject! = NSJSONSerialization.JSONObjectWithData(responSEObject as NSData,options: NSJSONReadingOptions.AllowFragments,error: nil)
- //打印数组或者字典元素
- println(arr!)
- //??避免强引用循环,可能由于OC版的AF 转化为 Swift代码时存在bug,使用弱引用或者无主引用,导致 self 为空;待完善
- //unowned var unSelf:AFRequestTest = self//
- //self.xxx
- //操作
- },failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
- println("请求错误Error: " + error.localizedDescription)
- unowned var unSelf:AFRequestTest = self
- //self.xxx操作
- //错误的相关操作
- })
- op.responseSerializer = AFHTTPResponseSerializer()
- op.start()
- }
- }
调用网络请求,在一个 ViewController的 ViewDidLoad中调用来测试
- override func viewDidLoad()
- {
- super.viewDidLoad()
- AFRequestTest().afRequestTest()
- }
输出:
- {
- result = (
- {
- state = {
- address = "\U4e2d\U56fd\U5317\U4eac\U5e02\U671d\U9633\U533a\U5efa\U5916\U8857\U9053\U5efa\U56fd\U8def100\U53f7";
- content = "\U4f60\U662f";
- date = "2015-01-07 19:06:13";
- fromUid = 5117873025;
- id = 6664;
- image = "http://xuyingtest-xuyingttt.stor.sinaapp.com/1420628745userImage.png";
- imagecount = 1;
- latitude = "39.907007";
- longitude = "116.470241";
- };
- },{
- state = {
- address = "\U4e2d\U56fd\U5317\U4eac\U5e02\U77f3\U666f\U5c71\U533a\U53e4\U57ce\U8857\U9053";
- content = "\U516c\U79ef\U91d1";
- date = "2014-11-23 11:10:57";
- fromUid = 5117873025;
- id = 5593;
- image = "http://xuyingtest-xuyingttt.stor.sinaapp.com/1416712253userImage.png";
- imagecount = 1;
- latitude = "39.897711";
- longitude = "116.184322";
- };
- },{
- state = {
- address = "";
- content = Chen;
- date = "2014-11-21 11:02:19";
- fromUid = 5117873025;
- id = 5551;
- image = "http://xuyingtest-xuyingttt.stor.sinaapp.com/1416538936userImage.png";
- imagecount = 1;
- latitude = "";
- longitude = "";
- };
- }
- );
- success = 0;
- }
注意事项和说明:
1.Swift和Oc调用方法的形式不同,Oc是空格,Swift是点号
2.Swift 网络请求的闭包和 Oc写法不太一样详见
3.避免强引用循环的语句
- unowned var unSelf:AFRequestTest = self
无主引用,不会增加引用计数;不能为nil
或者
- weak var self2:AFRequestTest! = self
弱引用,也不会增加引用计数;但返回的是可选类型,可以为nil
4.类型转换,向下转型
responSEObject asNSData
向下类型转换,使用 as 或者 as? ;
区别是 as?转换失败会返回nil;成功会返回可选类型
5.构造器和Java/C++等类似
- AFRequestTest().afRequestTest()
Swift的构造器相当于 Oc的 alloc init的合体,是用来初始化的.
不过 Swift的构造器是 init()其他语言的是 类名
6.不使用AF的JSON解析
- op.responseSerializer = AFHTTPResponseSerializer()
AFNetwoking的JSON解析对于某些接口可能会解析失败,导致 错误输出
Error: Request Failed: unacceptable content-type: text/html
这时我们只要加上面的代码,即可不让 AF帮我们解析;而使用系统的自带的JSON解析
7.Post请求类似,上述代码提供了一个测试的接口
Swift系列教程:http://www.jb51.cc/cata/272739