swift – 带参数的GET请求

前端之家收集整理的这篇文章主要介绍了swift – 带参数的GET请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于带参数的GET,哪种方式在 swift 3中重新定义?

示例:

https://my-side.com/data?token=AS7F87SAD84889AD/

提前致谢 !

@H_404_7@
@H_404_7@
示例如何将URLQueryItem用于请求.
func getRequest(params: [String:String]) {

    let urlComp = NSURLComponents(string: "https://my-side.com/data")!

    var items = [URLQueryItem]()

    for (key,value) in params {
        items.append(URLQueryItem(name: key,value: value))
    }

    items = items.filter{!$0.name.isEmpty}

    if !items.isEmpty {
      urlComp.queryItems = items
    }

    var urlRequest = URLRequest(url: urlComp.url!)
    urlRequest.httpMethod = "GET"
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: urlRequest,completionHandler: { (data,response,error) in
    })
    task.resume()
}


getRequest(params: ["token": "AS7F87SAD84889AD"])
@H_404_7@ 原文链接:https://www.f2er.com/swift/319571.html

猜你在找的Swift相关文章