我想從Alamofire的Swift 3中的url中獲取數據。單元格上沒有數據。我知道我在某處搞亂了某些東西,但我對swift 3相當陌生,無法看到問題,代碼如下:沒有數據從分析JSON與Alamofire Swift 3和Xcode 8測試版進入
JSON結構。
{
posts:
[
{
id: 「000000」,
url: "/content/interview",
date: "2016-11-03 09:01:41",
modified: "2016-11-03 09:03:47",
title: "An interview",
summary:
{
value: "<p>Lorem ipsum is simply dummy text</p> ",
format: "filtered_html"
}
]
}
夫特:
import UIKit
import Alamofire
class TableViewController: UITableViewController {
var titles = [String]()
var mainURL = "https://www.testapi.com"
typealias JSONstandard = [String : AnyObject]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: mainURL)
}
func callAlamo(url : String){
Alamofire.request(url).responseJSON(completionHandler: {
response in
self.parseData(JSONData: response.data!)
})
}
func parseData(JSONData : Data) {
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard
print(readableJSON)
if let post = readableJSON["posts"] as? JSONstandard {
if let posts = post["posts"] {
for i in 0..<posts.count {
let post = posts[i] as! JSONstandard
let title = post["title"] as! String
titles.append(title)
self.tableView.reloadData()
}
}
}
}
catch {
print(error)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = titles[indexPath.row]
return cell!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
上面帶來不數據變成 「小區」。任何幫助,將不勝感激。
謝謝,這很好!我想引入其他數據幾乎是一樣的原理。 – rob