Skip to content

Instantly share code, notes, and snippets.

@jozsef-vesza
Created June 3, 2014 22:20
Show Gist options
  • Save jozsef-vesza/1643dfe0df8d15f47b1a to your computer and use it in GitHub Desktop.
Save jozsef-vesza/1643dfe0df8d15f47b1a to your computer and use it in GitHub Desktop.
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