13封裝網路請求類庫

weixin_33782386發表於2017-08-16

新建檔案,選擇Cocoa Toutch Class NetworkTools


3245733-f91989b13ebdd52f.png

import UIKit

import Alamofire

enum MethodType {

case get

case post

}

class NetworkTools {

class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback :  @escaping (_ result : Any) -> ()) {

// 1.獲取型別

let method = type == .get ? HTTPMethod.get : HTTPMethod.post

// 2.傳送網路請求

Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { (response) in

// 3.獲取結果

guard let result = response.result.value else {

print(response.result.error)

return

}

// 4.將結果回撥出去

finishedCallback(result)

}

}

}

相關文章