//使用方法
let parameters = ["key1" : 1,"key2" : 2]
WBNetwork.shareInstance.request(requestType: .GET,url: port1,params: parameters,success: {(responSEObj) in
print(responSEObj!)
if responSEObj?["code"] as? Int == 0 {
}
}) {(error) in print(error!) }
//工具类
import UIKit
enum RequestType {
case GET
case POST
}
static let shareInstance : WBNetwork = {
let tool = WBNetwork()
tool.responseSerializer.acceptableContentTypes?.insert("text/html")
//tool.responseSerializer.acceptableContentTypes?.insert("text/html" as AnyHashable)
return tool
}()
func request(requestType: RequestType,url : String,params: [String : Any],success: @escaping([String : Any]?) ->(),failure: @escaping( _ error : Error?) -> ()){
//成功
let successBlock = { (task: URLSessionDataTask,responSEObj: Any?) in
success(responSEObj as? [String : Any])
}
//失败
let failureBlock = {(task : URLSessionDataTask?,error:Error) in
failure(error)
}
//GET
if requestType == .GET {
get(url,parameters: params,progress: nil,success: successBlock,failure: failureBlock)
//get(url,failure: failureBlock)
}
//POST
if requestType == .POST {
post(url,0)"> //post(url,failure: failureBlock)
}
}
}
原文链接:https://www.f2er.com/swift/322526.html