I'm trying to download JSON file from server & following an tutorial to do this (http://www.learnswiftonline.com/mini-tutorials/how-to-download-and-read-json/)
First I tried 'checking the response' part (I added some part to see what's wrong)
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()
This should print either "Everyone is fine~" or "Failed" but neither comes up... I tried to see statusCode so I put print(statusCode)
inside task
but again nothing is printed.
This is my screenshot of the playground:
+
CFRunLoop in Swift Command Line Program
This was the answer I was looking for, since I was dealing with OS X command line application (I moved the whole bunch to playground to see what would happen). Check this if you're the same with me