/ / Obtenir des données d'Api Swift [dupliquer] - json, swift

Obtenir des données de Api Swift [duplicate] - json, swift

J'essaie d'appeler Api de http://food2fork.com/about/api, mais quand je mets en oeuvre ce code, il montre des erreurs. Je fais comme ce tutoriel ici: https://www.topcoder.com/blog/calling-apis-parsing-json-with-swift/

Voici mon 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
}

}

Mais cela montre des erreurs comme ceci:

entrer la description de l'image ici

Toute aide serait appréciée.

Réponses:

1 pour la réponse № 1

Actualisé

Dans Swift 2, le JSONObjectWithData n'a pas le paramètre error.

Du 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