第一部分,配置项目
首先我们创建一个工程如下图
在此只讲纯手打拉第三方框架的方法
![](http://upload-images.jianshu.io/upload_images/671504-58ac5579565a3912.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后把下载的Alamofire
解压文件全部放进创建的项目文件夹中,如下图
![](http://upload-images.jianshu.io/upload_images/671504-5cc92c8b450098c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
关键时刻到了哦,集中精神,注意!!!
![](http://upload-images.jianshu.io/upload_images/671504-a4deb1b6cab4055d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这个图以后呢,就是最重要的时候了,请看好红色框框的部分,千万不要选择错了哦!!!
![](http://upload-images.jianshu.io/upload_images/671504-7794a648938c4572.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
接着我们要把
Alamofire
的静态库加到工程里面来,具体操作如下图![](http://upload-images.jianshu.io/upload_images/671504-0a7e10104a4cc2b1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
再然后呢,我们导入
Alamofire
的头文件,运行下,看是否报错,不报错呢,我们导入的文件配置工程就是对的!第二部分,Bison教你怎么使用Alamofire
在网上随便找了个接口,做为本教程的示范,具体代码如下
import UIKit
import Alamofire
class ViewController: UIViewController {
let BaiduURL = "http://apis.haoservice.com/lifeservice/cook/query?"
override func viewDidLoad() {
super.viewDidLoad()
//网络请求
self .reloadData()
}
func reloadData(){
let parameters = [
"menu": "土豆",
"pn": 1,
"rn": "10",
"key": "2ba215a3f83b4b898d0f6fdca4e16c7c",
]
Alamofire.request(.POST, BaiduURL, parameters:parameters ).responseJSON {response in
// debugPrint(response)
switch response.result {
case .Success:
//把得到的JSON数据转为字典
if let j = response.result.value as? NSDictionary{
//获取字典里面的key为数组
let Items = j.valueForKey("result")as! NSArray
//便利数组得到每一个字典模型
for dict in Items{
print(dict)
}
}
case .Failure(let error):
print(error)
}
}
}
}