Swift 3文件下载不起作用

时间:2016-11-20 14:21:22

标签: swift3

我没有Objective-c的经验,也没有Swift 3的新经验。我从这个链接中复制了代码:How to download file in swift?

这里是我在游乐场会话中复制的一段代码:

class Downloader {
    class func load(url: URL, to localUrl: URL, completion: @escaping () -> ()) {
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        let request = try! URLRequest(url: url, method: .get)

        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Success: \(statusCode)")
                }

                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl)
                    completion()
                } catch (let writeError) {
                    print("error writing file \(localUrl) : \(writeError)")
                }

            } else {
                print("Failure: %@", error?.localizedDescription);
            }
        }
        task.resume()
    }
}

错误显示:

playgroung errors picture

导入Fondation代码行现在出错:

error with the line of code import Fondation

0 个答案:

没有答案