美文网首页
swift网络请求Alamofire

swift网络请求Alamofire

作者: young_lei | 来源:发表于2016-04-28 10:48 被阅读0次

swift网络请求Alamofire

1.首先使用cocoa pods导入Alamofire

2.自己写的封装 继承NSObject

/**

*  请求 JSON

*

*  @param method        HTTP 请求方法

*  @param URLString     URL字符串

*  @param parameters    参数字典

*  @param completion    完成回调,返回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.用的时候直接调用  response返回的是个字典

NetworkTool.requestJSON(.POST, URLString: url, parameters: nil) { (response) in

}

相关文章

网友评论

      本文标题:swift网络请求Alamofire

      本文链接:https://www.haomeiwen.com/subject/zefhrttx.html