我在解析Swift时遇到问题,它说我无法将[Dictionary]类型的值转换为类型[String:AnyObject] 这是导致错误无法转换类型的值 - Swift
self.grabData(json: json as [String : AnyObject])
这是整个代码。谢谢!
func jsonParser() {
let urlPath = "http://csmadison.dhcp.bsu.edu/~vjtanksale/cs320/selectstudents.php"
guard let endpoint = URL(string: urlPath) else {
print("Error creating endpoint")
return
}
let request = URLRequest(url: endpoint)
URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
guard let data = data else {
return
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [Dictionary<String, Any?>] else {
print("No idea")
return
}
print(json)
self.grabData(json: json as [String : AnyObject]) // This is where I get the error
} catch let error as NSError {
print(error.debugDescription)
}
}.resume()
}
func grabData(json: [String:AnyObject]) {
guard let searchResult = SearchResult(json: json) else {
return
}
self.results.append(searchResult)
DispatchQueue.main.async {
self.listTableView.reloadData()
}
}
这个固定的问题。然而,我的结果字典在这之后是空的。只有json有数据。 – tncb
循环时'result'为空吗?或者你的'self.results'是空的? –
他们都是空的。我相信在json循环中的结果不会附加它。 – tncb