我尝试使用for循环和Google Places API来查找地址ID数组,如下所示。代码大部分没有错误,但是,我要么关闭我的闭包或上面的错误。我不确定此刻它是什么 - 非常感谢任何协助!
let mexican = ["ChIJM8S9ACvoIogRmzYiLgzlP_g",
"ChIJwSHwoNTpIogRyElYj-kHC14",
"ChIJDWRMYifoIogR7S6U4tryodY",
"ChIJqSVd6yfoIogRmTgBSoO4Z0k",
"ChIJLacWlinoIogRVGYk-bZbGUE",
"ChIJSUC9S2zCIogRTKyiW0q_6tI"]
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()
var place: [String]
var results: Array<Any>
switch index.row
{
case 0 :
place = international
case 1 :
place = american
default :
place = mexican
}
for id in place
{
placesClient.lookUpPlaceID(id, callback: { (result, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
guard let result = result
else
{
print("No place details for \(id)")
return
}
results.append(result)
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:2)
错误是因为您没有初始化结果数组, 简单的解决方案可以是这个
像这样声明你的结果数组
var results = [Any]()
我认为你应该这样做:
你应该在viewDidLoad
方法
var results = [Any]()
然后在闭包内使用此变量,如下所示,因为我认为您需要在代码的其他部分中使用此结果
self.results.append(result)