-2

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:

enter image description here

Any help would be appreciate.

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116

1 Answers1

1

Updated

In Swift 2, the JSONObjectWithData does not have the parameter error.

From the documentation:

+ JSONObjectWithData:options:error:
Returns a Foundation object from given JSON data.

Declaration
SWIFT
class func JSONObjectWithData(_ data: NSData,
                      options opt: NSJSONReadingOptions) throws -> AnyObject
OBJECTIVE-C
+ (id)JSONObjectWithData:(NSData *)data
                 options:(NSJSONReadingOptions)opt
                   error:(NSError * _Nullable *)error
Renzo
  • 26,848
  • 5
  • 49
  • 61
  • Mr.Renzo please check it again, even though I declare it. It still error, please see my update question. – Twitter khuong291 Jan 09 '16 at 11:07
  • @khuong291, which swift are you using? – Renzo Jan 09 '16 at 11:16
  • I am using swift 2, xcode 7 – Twitter khuong291 Jan 09 '16 at 11:18
  • Then, [`JSONObjectWithData`](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/JSONObjectWithData:options:error:) has the parameter error only for Objective-C, and not for swift (where it can throws an exception). – Renzo Jan 09 '16 at 11:36