Created
August 25, 2015 16:53
-
-
Save pachun/c8c301e22473c293b9fc to your computer and use it in GitHub Desktop.
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 characters
| import UIKit | |
| @UIApplicationMain | |
| class AppDelegate: UIResponder, UIApplicationDelegate { | |
| var window: UIWindow? | |
| func addFoodTruck(foodTruckOptional: Result<FoodTruck>) { | |
| switch(foodTruckOptional) { | |
| case let .Error(error): | |
| println(error.description) | |
| case let .Value(boxedFoodTruck): | |
| let foodTruck = boxedFoodTruck.value | |
| println(foodTruck.name) | |
| } | |
| } | |
| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
| let url: NSURL! = NSURL(string: "https://rocky-ridge-2525.herokuapp.com/food_trucks") | |
| let getFoodTrucksRequest = NSURLRequest(URL: url) | |
| let getFoodTrucksTask = NSURLSession.sharedSession().dataTaskWithRequest(getFoodTrucksRequest) { data, urlResponse, error in | |
| // check for HTTP request errors | |
| if let err = error { | |
| self.addFoodTruck(.Error(err)) | |
| } | |
| var jsonError: NSError? | |
| let jsonOptional: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &jsonError) | |
| // check for json parse errors | |
| if let err = jsonError { | |
| self.addFoodTruck(.Error(err)) | |
| } | |
| if let jsonFoodTruckArray = JSONArray(jsonOptional) { | |
| for jsonFoodTruck in jsonFoodTruckArray { | |
| if let foodTruckDictionary = jsonFoodTruck >>> JSONDictionary { | |
| let foodTruck = FoodTruck.create <^> | |
| foodTruckDictionary["name"] >>> JSONString <*> | |
| foodTruckDictionary["day"] >>> JSONString <*> | |
| foodTruckDictionary["time"] >>> JSONString <*> | |
| foodTruckDictionary["location"] >>> JSONString | |
| } | |
| } | |
| } | |
| self.addFoodTruck(.Error(NSError())) | |
| } | |
| getFoodTrucksTask.resume() | |
| return true | |
| } | |
| } | |
| func JSONString(object: JSON) -> String? { | |
| return object as? String | |
| } | |
| func JSONDictionary(object: JSON) -> JSONDictionaryType? { | |
| return object as? JSONDictionaryType | |
| } | |
| func JSONArray(object: JSON) -> JSONArrayType? { | |
| return object as? JSONArrayType | |
| } | |
| typealias JSON = AnyObject | |
| typealias JSONDictionaryType = Dictionary<String, JSON> | |
| typealias JSONArrayType = Array<JSON> | |
| struct FoodTruck { | |
| let name: String | |
| let day: String | |
| let time: String | |
| let location: String | |
| static func create(name: String)(day: String)(time: String)(location: String) -> FoodTruck { | |
| return FoodTruck(name: name, day: day, time: time, location: location) | |
| } | |
| } | |
| enum Result<A> { | |
| case Error(NSError) | |
| case Value(Box<A>) | |
| } | |
| final class Box<A> { | |
| let value: A | |
| init(value: A) { | |
| self.value = value | |
| } | |
| } | |
| 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 | |
| } | |
| } | |
| infix operator <^> { associativity left } | |
| func <^><A, B>(a: A?, f: A -> B) -> B? { | |
| if let x = a { | |
| return f(x) | |
| } else { | |
| return .None | |
| } | |
| } | |
| infix operator <*> { associativity left } | |
| func <*><A, B>(f: (A -> B)?, a: A?) -> B? { | |
| if let x = a { | |
| if let fx = f { | |
| fx(x) | |
| } | |
| } | |
| return .None | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment