我从2015年开始做一个教程,对swift非常新,我从[AnyHashable : Any]?
行获得了self.objects
类型没有下标成员错误。
Moltin.sharedInstance().product.listing(withParameters: nil, success: { (responseDictionary)-> Void in
//Assign Products array to object property
self.objects = responseDictionary["result"] as! [AnyObject]
}) { (responseDictionary, error) in
print("Something went wrong!")
}
}
答案 0 :(得分:2)
重构您的代码以将响应正确地转换为字典。
Moltin.sharedInstance().product.listing(withParameters: nil, success: { (response) -> Void in
//Assign Products array to object property
guard let responseDictionary = response as? [String: AnyObject] else {
return
}
self.objects = responseDictionary["result"] as! [AnyObject]
print(self.objects)
//Tell the table view to reload it's data
self.tableView.reloadData()
}) { (responseDictionary, error) in
print("Something went wrong!")
}
我尝试在此之后运行您的代码,它可以正常工作。 API没有返回您期望的内容。我们来看看......
let pagination = responseDictionary["pagination"]!
let results = responseDictionary["result"] as! [AnyObject]
print(pagination.count) // 8
print(results.count) // 0
如您所见,pagination
键下有8个值,但results
键下有0。这意味着解析逻辑是正确的。如果它在API端有错误。
API不会返回任何结果,这就是表为空的原因。
运气不错,我希望这会有所帮助!