【发布时间】:2019-02-11 15:14:57
【问题描述】:
我一直在我的视图控制器中获取 JSON,我需要一个函数来将同一个 VC 中的数据添加到 firebase,因此导入了 Firebase(Pods firebase core、auth 和 firestore),现在它在获取 JSON 时出现错误这是“下标”的模棱两可的使用
func getDetails(link: URL!) {
var plot : String = " "
let task = URLSession.shared.dataTask(with: link!) { (data, response, error) in
if error != nil
{
print("error")
}
else
{
if let content = data
{
do
{
//JSON results
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableLeaves) as AnyObject
//myJson ~~~ ["Plot"] Ambiguous use of 'subscript'
plot = myJson["Plot"] as! String
}
catch
{
print("error in JSONSerialization")
}
}
}
}
task.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.plot.text = plot
})
}
我希望保持能够选择 JSON 的“Plot”值并让 firebase 运行
【问题讨论】:
-
我建议从
JSONSerialization移到Codable,这样您就可以获得真正的模型对象,而不必再担心字符串作为下标。通过粘贴 JSON 响应,使用 app.quicktype.io 作为模型对象定义的起点。另外,不要使用计时器来等待结果出来,这就是我们DispatchGroup的用途。 -
为什么要将 json 转换为
AnyObject? ???as AnyObject- 删除它... -
@RobertDresler 因为它会说 Type 'Any' has no subscript members
-
@LukasBaranauskas
as! [String:Any]怎么样? ??? (在实际代码中避免强制解包,而是使用例如可选绑定安全地解包) -
@RobertDresler 谢谢老兄!
标签: json swift firebase google-cloud-firestore