前端之家收集整理的这篇文章主要介绍了
使用AFNetworking在Swift中请求数据,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
继承:AFHTTPSessionManager
import AFNetworking
static let tools: NetworkTools = {
let baseUrl = NSURL(string: "https://xxx.xxx.xxx/")
let tools = NetworkTools(baseURL: baseUrl)
tools.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json","text/json","text/javascript","text/plain") as? Set<String>
return tools
}()
class func shareNetworkTools() -> NetworkTools {
return tools
}
请求数据
class func loadData(finished: (models:[User]?,error: NSError?) -> ()) {
let path = "baseUrl要拼接的路径"
let params = ["userId": userId]
NetworkTools.shareNetworkTools().GET(path,parameters: params,progress: { (_) -> Void in
},success: { (_,JSON) -> Void in
print(JSON)
finished(models: models,error: nil)
}) { (_,error) -> Void in
print(error)
finished(models: nil,error: error)
}
}
Request.loadData { (models,error) -> () in
if error != nil {
return
}
//打印数据模型
printf(models)
}
原文链接:https://www.f2er.com/swift/324024.html