Created
June 3, 2014 22:20
-
-
Save jozsef-vesza/1643dfe0df8d15f47b1a to your computer and use it in GitHub Desktop.
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
class MainViewModel: NSObject { | |
var model :Meal[] = [] | |
let mealUrl = "http://verdant-upgrade-554.appspot.com/soservices/mealmanager" | |
init() { | |
super.init() | |
} | |
func fetchModel(completion:() -> Void) { | |
self.downloadMeals({ completion() }) | |
} | |
func downloadMeals (completion:() -> Void) { | |
var request = NSURLRequest(URL: NSURL(string: self.mealUrl)) | |
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in | |
var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil) as NSArray | |
for obj : AnyObject in result { | |
var dict = obj as Dictionary<String, String> | |
var meal = Meal() | |
meal.name = dict["name"]! | |
self.model.append(meal) | |
} | |
completion() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment