placesClient给出了一个保持零值的错误,在阅读了google places文档后,我读到了这句话:
应该通过[GMSPlacesClient sharedClient]方法访问此类。
我想知道如何实现sharedClient方法,以便根据给定位置信息的placesID运行搜索?
var placesClient: GMSPlacesClient!
func lookUpPlaceID() {
let placeID = "ChIJPXw5JDZM6IAR4rTXaFQDGys"
//What value should placesClient be holding if not GMSPlacesClient in order to run the search based on the placeID?
self.placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}
if let place = place {
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)")
} else {
print("No place details for \(placeID)")
}
})
}
答案 0 :(得分:3)
sharedClient
是Objective C中的类方法。在Swift中,您将其视为Type Method。因此,不要将placesClient
分配给GMSPlacesClient
类型,而是将其分配给sharedClient
类型方法的结果:
let placesClient = GMSPlacesClient.sharedClient()
placesClient
现在将拥有GMSPlacesClient
的实例(一个共享实例,稍后调用sharedClient
将返回相同的实例,如果它还没有垃圾收集),而不是像原始代码一样持有类型。