2016-02-16 26 views
3

我试图从服务器&下载JSON文件下面的教程来做到这一点(http://www.learnswiftonline.com/mini-tutorials/how-to-download-and-read-json/正从URL数据斯威夫特

首先我想“检查响应”部分(我加了一些部分,看看有什么错)

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() 

这应该打印或者“每个人都精〜”或“失败”,但也不来了......我想看到的StatusCode所以我把里面print(statusCode)task再次打印什么。

这是我的操场截图:

enter image description here

+

CFRunLoop in Swift Command Line Program

这就是我一直在寻找的答案,因为我正在处理OS X命令行应用程序(我把整个一堆搬到了操场上,看看会发生什么)。检查这个,如果你与我相同

+0

非常感谢您的编辑! :) – Joy

回答

2

你看不到任何东西,因为dataTaskWithRequest是异步的,你的操场在'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 

这篇文章可能有助于澄清更多: How do I run Asynchronous callbacks in Playground

编辑:

在Sw ift 3,这个改变了一下。

import PlaygroundSupport 
PlaygroundPage.current.needsIndefiniteExecution = true 
+0

哇,我一定早就问过了...... :)肯定有效。非常感谢! – Joy

+0

是的,继续询问。 –

0

工作正常,在我的项目后,我改变了“HTTP” - > HTTPS,苹果宣布“应用传输安全”为iOS 9和OSX 10.11埃尔卡皮坦

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响应:

<NSHTTPURLResponse: 0x7a670300> { 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)"}}) 
+0

虽然我解决了与其他答案...仍然非常感谢! :) – Joy