Created
December 9, 2016 07:36
-
-
Save todbot/bc13cfa0189c95d3cf172c64168a1326 to your computer and use it in GitHub Desktop.
CrashSpace Sign JSON parsing in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: CrashSpace Sign JSON parsing in Swift | |
import Cocoa | |
import Foundation | |
let urlString = "https://crashspacela.com/sign2/json/"; | |
let url = URL(string: urlString) | |
URLSession.shared.dataTask(with:url!) { (data, response, error) in | |
if error != nil { | |
print("oh no \(error)") | |
} else { | |
print("hi there; we got server response") | |
do { | |
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [Any] | |
let lastItem = parsedData[0] as! [String:Any] | |
print("lastItem = \(lastItem)") | |
let id = lastItem["id"] as! String | |
let msg = lastItem["msg"] as! String | |
let diff_mins = lastItem["diff_mins_max"] as! Int | |
print("lastItem: id:\(id), msg:\(msg), diff_mins:\(diff_mins)") | |
} catch let error as NSError { | |
print(error) | |
} | |
} | |
}.resume() | |
// in playground, loop forever so the task above completes | |
var waiting = true | |
while( waiting ) { | |
sleep(10) | |
print("sleeping...") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment