Created
September 29, 2014 08:34
-
-
Save jseanj/5c77e02b62a44d1b7218 to your computer and use it in GitHub Desktop.
Revisions
-
jseanj created this gist
Sep 29, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ import UIKit import XCPlayground XCPSetExecutionShouldContinueIndefinitely() typealias JSON = AnyObject typealias JSONDictionary = [String: JSON] typealias JSONArray = [JSON] let url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk" let request = NSURLRequest(URL: NSURL.URLWithString(url)) final class Box<A> { let value: A init(_ value: A) { self.value = value } } struct Weather { let id: Int let name: String static func create(id: Int)(name: String) -> Weather { return Weather(id: id, name: name) } } enum Result<A> { case Error(NSError) case Value(Box<A>) } func JSONString(object: JSON?) -> String? { return object as? String } func JSONInt(object: JSON?) -> Int? { return object as? Int } func JSONObject(object: JSON?) -> JSONDictionary? { return object as? JSONDictionary } infix operator >>> {associativity left precedence 150} func >>><A, B>(a: A?, f: A->B?) -> B? { if let x = a { return f(x) } else { return .None } } func getData(request: NSURLRequest, callback: (Result<Weather>) -> ()) { let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, urlResponse, error in if let err = error { callback(Result.Error(err)) return } var jsonErrorOptional: NSError? let jsonOptional: JSON! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &jsonErrorOptional) // println(jsonOptional) if let err = jsonErrorOptional { callback(Result.Error(err)) return } if let json = jsonOptional >>> JSONObject { if let id = json["id"] >>> JSONInt { if let name = json["name"] >>> JSONString { let weather = Weather(id: id, name: name) callback(Result.Value(Box(weather))) return } } } } task.resume() } getData(request) { result in switch result { case let .Error(error): println(error) case let .Value(boxedUser): let weather = boxedUser.value println(weather.id) println(weather.name) } }