看了几天swift的教学视频
教学视频中有对Alamofire的请求进行简单的封装
就记录下来方便以后查阅
1.首先当然是导入Alamofire
pod'Alamofire'
2.接下来就是建立工具类了
import UIKit
//MARK:这个地方 一定要导入Alamofire 不然我们怎么用啊
import Alamofire
//MARK:我们自己写一个枚举类型,用来区分
enum METHODTYPE {
case GET
case POST
}
//MARK:网络工具(单例)
class NetWorkUtilsSwift{
//单例
static let shareTools:NetWorkUtilsSwift = {
let tools = NetWorkUtilsSwift()
return tools
}()
}
//MRRK - 封装 AFN 网络方法
extension NetWorkUtilsSwift{
func request(methodType:METHODTYPE,urlString:String,parameters:[String:String]? = nil,finishCallBack:@escaping (_ responseObject:AnyObject?, _ error:Error?) -> ()){
let method = methodType == .GET ? HTTPMethod.get : HTTPMethod.post;
Alamofire.request(urlString, method: method, parameters: parameters).responseJSON { (response) in
if response.result.isSuccess
{
finishCallBack(response.result.value as AnyObject?,nil)
}
else
{
finishCallBack(nil,response.result.error)
}
}
}
}
网友评论