从Swift中的URL获取数据

我正在尝试从服务器下载JSON文件并按照教程进行操作( http://www.learnswiftonline.com/mini-tutorials/how-to-download-and-read-json/ )

首先,我尝试“检查响应”部分(我添加了一些部分,看看有什么问题)

let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) { (data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode if (statusCode == 200) { print("Everyone is fine, file downloaded successfully.") } else { print("Failed") } task.resume() 

这应该打印“Everyone is fine~”或“Failed”但是没有出现…我试图看到statusCode所以我把print(statusCode)放在task但是再没有打印出来。

这是我操场的截图:

在此处输入图像描述

+

Swift命令行程序中的CFRunLoop

这是我正在寻找的答案,因为我正在处理OS X命令行应用程序(我把整个团队移动到游乐场看看会发生什么)。 如果你和我一样,请检查一下

你看不到任何东西,因为dataTaskWithRequest是异步的,你的游乐场就在’task.resume()`之后停止。 异步任务不会使更改运行。

你可以在task.resume之后task.resume

 XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 

也’导入XCPlayground’,如下所示:

 import Foundation import XCPlayground let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) {(data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode if (statusCode == 200) { print("Everyone is fine, file downloaded successfully.") } else { print("Failed") } } task.resume() XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 

这篇文章可能会澄清更多: 如何在Playground中运行异步回调

编辑:

在Swift 3中,这改变了一点。

 import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true 

在我更改了“http” – > https后,Apple在我的项目中工作正常,Apple为iOS 9和OSX 10.11 El Capitan宣布了“App Transport Security”

  let requestURL: NSURL = NSURL(string: "https://www.learnswiftonline.com/Samples/subway.json")! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) { (data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse let statusCode = httpResponse.statusCode if (statusCode == 200) { print("Everyone is fine, file downloaded successfully.") } else { print("Failed") } } task.resume() 

http响应:

   { URL: https://www.learnswiftonline.com/Samples/subway.json } { status code: 200, headers { "Content-Encoding" = gzip; "Content-Type" = "application/json"; Date = "Tue, 16 Feb 2016 07:43:05 GMT"; "Last-Modified" = "Sat, 14 Nov 2015 08:21:26 GMT"; Server = "cloudflare-nginx"; "Set-Cookie" = "__cfduid=d9d92befe8746168b2b291f5bfb8996081455608585; expires=Wed, 15-Feb-17 07:43:05 GMT; path=/; domain=.learnswiftonline.com; HttpOnly"; "cf-ray" = "27579e989ad5208a-KIX"; 

}}

或使用http时出错

  Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSErrorFailingURLStringKey=http://www.learnswiftonline.com/Samples/subway.json, NSErrorFailingURLKey=http://www.learnswiftonline.com/Samples/subway.json, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSUnderlyingError=0x7866de20 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}})