尝试这样做:
//replace
let jsonData:NSArray = NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers , error: &err) as? NSArray
//with
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
然后 print(json) 看看你得到什么样的对象
这是我用来发出获取请求的一些代码:
let url = NSURL(string: urlString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler:{
(data, response, error) in
if error != nil {
//Handle error, just for testing I do this:
print(error!.localizedDescription)
} else {
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
print(json)
//use data from json, if it's a dictionary, you can loop through and add objects to an array
}
})
task!.resume()
Here's关于提出请求的答案
这是一个简单的 cellForRowAtIndexPath 方法,它从一个数组中填充 tableViewCells:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell")
let text = self.tableContents[indexPath.row] as! String
//tableContents is just the array from which you're getting what's going in your tableView, and should be declared outside of your methods
cell.textLabel?.text = text
return cell
}
在 viewDidLoad 中有这个 self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")