Api 调用工具类 NetApiTools
import UIKit
import Alamofire
enum MethodType {
case GET
case POST
}
class NetApiTool {
class func requestData(type : MethodType,URLString : String,parametes : [String : NSString]? = nil,callback : @escaping (_ result : AnyObject)-> ()){
let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
AF.request(URLString,method: method,parameters: parametes).responseData { response in
switch response.result {
case .success(let value):
//如果成功则回调数据
callback(value as AnyObject)
case .failure(let error):
print(error)
}
}
}
}
----业务方法调用
func requestData (){
//请求第一部分的推荐数据
let parameters = ["limit" : "4","offset" : "0","time" : NSDate.getCurrentTime() as NSString]
//请求第二部分的颜值数据
//请求后面部分的直播房间数据
NetApiTool.requestData(type: .GET, URLString: "http://xx.xxx.cn/api/v1/getHotCate", parametes: parameters) { [self] result in
print("打印结果")
print( String(data: (result as! Data), encoding: .utf8))
}
}