【发布时间】:2018-06-20 06:33:09
【问题描述】:
我是 Swift 的新手,希望得到您的帮助。我正在做一个像这样快速解析 JSON 数据的应用程序: Project 这是我写的代码
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var albumArray = [AnyObject]()
var url = ("https://jsonplaceholder.typicode.com/photos")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Alamofire.request("https://jsonplaceholder.typicode.com/photos").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
//
if let resData = swiftyJsonVar[].arrayObject {
self.albumArray = resData as [AnyObject]; ()
}
if self.albumArray.count > 0 {
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return albumArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CostumTableViewCell
let title = albumArray[indexPath.row]
cell?.titleLabel?.text = title["title"] as? String
return cell!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
问题是没有任何错误但不显示数据。您还可以看到 JSON:https://jsonplaceholder.typicode.com/photos
【问题讨论】:
-
你能更准确地解释问题的“不起作用”部分吗?
-
@KeyurTailor 不显示数据。对不起
-
你试过在控制台打印数据吗?
-
@KeyurTailor 是的,控制台中的数据打印成功
-
那我建议你检查一下你收到的数据的格式。
标签: json swift alamofire swift4