Swift http 请求使用 urlSession

编程入门 行业动态 更新时间:2024-10-11 19:17:49
本文介绍了Swift http 请求使用 urlSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为我的服务器编写用于 HTTP 请求的 func 并获取一些数据,当我打印它时 (print(responseString)) 看起来不错,但是当我尝试返回数据时,它总是空的

I want to write func for HTTP Request to my server and get some data, when i print it (print(responseString)) it looks good, but when i try to return data, its always empty

public func HTTPRequest(dir: String, param: [String:String]?) -> String{ var urlString = HOST + dir + "?" var responseString = "" if param != nil{ for currentParam in param!{ urlString += currentParam.key + "=" + currentParam.value + "&" } } let url = URL(string: urlString) let task = URLSession.shared.dataTask(with: url!) { data, response, error in guard error == nil else { print("ERROR: HTTP REQUEST ERROR!") return } guard let data = data else { print("ERROR: Empty data!") return } responseString = NSString(data: data,encoding: String.Encoding.utf8.rawValue) as! String print(responseString) } task.resume() return responseString }

推荐答案

您需要使用完成块而不是返回值,因为 dataTask 闭包是异步运行的,即稍后,在您返回之后你的方法.您不想尝试立即返回该值(因为您还没有它).您想 (a) 将此函数更改为不返回任何内容,但 (b) 提供一个完成处理程序闭包,您将在 dataTask 闭包中调用该闭包,在该闭包中构建 responseString.

You need to use completion block instead of returning value because the dataTask closure is run asynchronously, i.e. later, well after you return from your method. You don't want to try to return the value immediately (because you won't have it yet). You want to (a) change this function to not return anything, but (b) supply a completion handler closure, which you will call inside the dataTask closure, where you build responseString.

例如,您可以这样定义它:

For example, you might define it like so:

public func HTTPRequest(dir: String, param: [String:String]? = nil, completionHandler: @escaping (String?, Error?) -> Void) { var urlString = HOST + dir if let param = param { let parameters = param.map { return $0.key.percentEscaped() + "=" + $0.value.percentEscaped() } urlString += "?" + parameters.joined(separator: "&") } let url = URL(string: urlString) let task = URLSession.shared.dataTask(with: url!) { data, response, error in guard let data = data, error == nil else { completionHandler(nil, error) return } let responseString = String(data: data, encoding: .utf8) completionHandler(responseString, nil) } task.resume() }

请注意,我正在使用以下内容对参数字典中的值进行百分比转义:

Note, I'm percent escaping the values in the parameters dictionary using something like:

extension String { /// Percent escapes values to be added to a URL query as specified in RFC 3986 /// /// This percent-escapes all characters besides the alphanumeric character set and "-", ".", "_", and "~". /// /// www.ietf/rfc/rfc3986.txt /// /// - Returns: Returns percent-escaped string. func percentEscaped() -> String { let allowedCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~") return self.addingPercentEncoding(withAllowedCharacters: allowedCharacters)! } }

然后你会这样称呼它:

HTTPRequest(dir: directory, param: parameterDictionary) { responseString, error in guard let responseString = responseString else { // handle the error here print("error: (error)") return } // use `responseString` here DispatchQueue.main.async { // because this is called on background thread, if updating // UI, make sure to dispatch that back to the main queue. } } // but don't try to use `responseString` here

更多推荐

Swift http 请求使用 urlSession

本文发布于:2023-11-26 04:30:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   http   urlSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!