我正试图从一个API获取数据,但是没有显示数据。
Alamofire.request(https://jsonplaceholder.typicode.com/comments, method: .get, encoding: URLEncoding.default, headers: nil).validate(statusCode: 200..<300).responseJSON {
response in
//getting json
if let json = response.result.value {
print("typppppe:\(type(of: json))")
//converting json to NSArray
let commentArray = json as! NSArray
let dictName = commentArray["name"].value
let dictBody = commentArray["body"].stringValue as? String ?? ""
print("namessss\(dictName)")
print("bodyssss\(dictBody)")
print("commm:\(commentArray)")
let comment = Comment.init(commentName: dictName, commentBody: dictBody)
self.comments.append(comment)
self.commentTableJson.reloadData()
}
}
这个函数的问题是如何修复以便显示
tableViewCell
中的数据。 最佳答案
必须对注释json运行循环。
let str = "https://jsonplaceholder.typicode.com/comments"
let url = URL(string: str)
Alamofire.request(url! , method: .get).responseJSON { (dataResponse) in
print(dataResponse.result.value)
if dataResponse.result.isSuccess {
if let arrCommentsJson = dataResponse.result.value! as? [[String: Any]] {
for comment in arrCommentsJson {
let dictName = comment["name"]
let dictBody = comment["body"]
let comment = Comment.init(commentName: dictName, commentBody: dictBody)
self.comments.append(comment)
}
self.commentTableJson.reloadData()
}
}
}
关于ios - tableView不显示我的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51577248/