swift - OC与swift3.0混编的项目中使用AFN

前端之家收集整理的这篇文章主要介绍了swift - OC与swift3.0混编的项目中使用AFN前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//使用方法

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

}


class WBNetwork: AFHTTPSessionManager {

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)

}

}

}

猜你在找的Swift相关文章