I want to change the url of an nsurlsession call on click of a button and refresh the tableView immediately here is what i have so far
@IBAction func d(sender: AnyObject) {
url = NSURL(string: "http://www.json-generator.com/api/json/get/coDxrSkrNe?indent=2")!
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
if error != nil {
print(error!)
} else {
do {
if let restaurants = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]] {
let names = restaurants.flatMap { $0["preferred_name"] as? String }
let time = restaurants.flatMap { $0["cooking_time"] as? String }
for restaurant in restaurants {
if let name = restaurant["preferred_name"] as? String {
let cookingTime = ""
let food = Food(name: name, time:cookingTime)
self.food.append(food)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
print(names)
}
} catch let error as NSError {
print(error)
}
}
}.resume()
self.tableView.reloadData()
}
Here is what my code looks like now but the tableView is still not reloading or changing the data after clicking the button