从URL获取数据作为单独的函数

时间:2017-05-10 11:32:30

标签: ios swift function

我尝试创建函数以从URL获取数据:

func getStringFromUrl(urlString: String) -> String {
    if let requestURL = URL(string: urlString) {
        let session = URLSession(configuration: URLSessionConfiguration.default)
        let task = session.dataTask(with: requestURL, completionHandler: { (data, response, error) in
            if let data = data {
                do {
                    let str = String(data: data, encoding: String.Encoding.utf8)
                    return str
                }
                catch let error as NSError {
                    print ("error = \(error)")
                }

            }
            else {
               print ("error = \(error)")
            }
        })
        task.resume()
    }
}

但是我收到了这个错误: void函数中出现意外的非void返回值

如何创建单独的函数以从Url获​​取数据?

1 个答案:

答案 0 :(得分:2)

在您的代码中,您有:

- laravel/passport v1.0.1 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- laravel/passport v1.0.2 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- laravel/passport v1.0.3 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- laravel/passport v1.0.4 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- laravel/passport v1.0.5 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- laravel/passport v1.0.6 requires symfony/psr-http-message-bridge ^0.3.0 -> satisfiable by symfony/psr-http-message-bridge[v0.3].
- Conclusion: don't install symfony/psr-http-message-bridge v0.3
- Can only install one of: symfony/psr-http-message-bridge[v0.2, v0.4].
- Can only install one of: symfony/psr-http-message-bridge[v0.4, v0.2].
- Can only install one of: symfony/psr-http-message-bridge[v0.2, v0.4].
- laravel/passport v1.0.0 requires symfony/psr-http-message-bridge ^0.2.0 -> satisfiable by symfony/psr-http-message-bridge[v0.2].
- Installation request for laravel/passport ^1.0 -> satisfiable by laravel/passport[v1.0.0, v1.0.1, v1.0.2, v1.0.3, v1.0.4, v1.0.5, v1.0.6].
- Installation request for symfony/psr-http-message-bridge (locked at v0.4) -> satisfiable by symfony/psr-http-message-bridge[v0.4].

哪个在闭包块内,未定义为返回任何内容。因为函数let str = String(data: data, encoding: String.Encoding.utf8) return str 是异步任务,所以它不会立即返回。您应该使用完成块/闭包来获取返回时的响应。还要记住它可能不会返回,因此字符串必须是可选的。请参阅下面的代码。

session.dataTask

编辑:用法

func getStringFromUrl(urlString: String, completion: @escaping (_ str: String?) -> Void) {
    if let requestURL = URL(string: urlString) {
        let session = URLSession(configuration: URLSessionConfiguration.default)
        let task = session.dataTask(with: requestURL, completionHandler: { (data, response, error) in
            if let data = data {
                do {
                    let str = String(data: data, encoding: String.Encoding.utf8)
                    completion(str)
                }
                catch let error as NSError {
                    print ("error = \(error)")
                    completion(nil)
                }

            }
            else {
                print ("error = \(error)")
                completion(nil)
            }
        })
        task.resume()
    }
}