swift網(wǎng)絡(luò)請求Alamofire
1.首先使用cocoa pods導(dǎo)入Alamofire
2.自己寫的封裝 繼承NSObject
/**
* ?請求 JSON
*
* ?@param method ? ? ? ?HTTP 請求方法
* ?@param URLString ? ? URL字符串
* ?@param parameters ? ?參數(shù)字典
* ?@param completion ? ?完成回調(diào)坯临,返回NetworkResponse
*/
class func requestJSON(method: Alamofire.Method, URLString: String, parameters: [String: String]? = nil, completion:(response: AnyObject) -> ()) {
Alamofire.request(method,URLString,parameters: parameters,encoding: .URL,headers: nil).responseJSON{(JSON)in
switch JSON.result{
case .Success:
if let value = JSON.result.value {
completion(response: value)
}
case .Failure(let error):
debugPrint(error)
}
}
}
3.用的時候直接調(diào)用 ?response返回的是個字典
NetworkTool.requestJSON(.POST, URLString: url, parameters: nil) { (response) in
}