I am trying to call Api from http://food2fork.com/about/api
, but I when I implement these code, it shows errors. I am doing like this tutorial here: https://www.topcoder.com/blog/calling-apis-parsing-json-with-swift/
Here is my code:
import UIKit
import Alamofire
import SwiftyJSON
class ListDishsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var dishsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
print("hello")
let logo = UIImage(named: "bento")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
dishsTableView.dataSource = self
dishsTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loadDishesData() {
let urlPath = "http://food2fork.com/api/search?key=67fd12776ee242546ac92d3122dabbd9&q=shredded%20chicken"
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if error != nil {
print(error!.localizedDescription)
}
var error: NSError?
let jsonResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary
if error != nil {
// If there is an error parsing JSON, print it to the console
print("JSON Error \(error!.localizedDescription)")
}
})
task.resume()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DishCell", forIndexPath: indexPath) as! ListDishsTableViewCell
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
}
But it shows errors like this:
Any help would be appreciate.