placesClient - 正确使用 sharedClient 方法?

placesClient - properly use sharedClient method?

placesClient 给出了一个持有 nil 值的错误,在阅读 google places 文档后我读到了这个声明:

This class should be accessed through the [GMSPlacesClient sharedClient] method.

我想知道如何实现 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)")
        }
    })


}

sharedClient 是 Objective C 中的 class 方法。在 Swift 中,您将其视为 Type Method。因此,不是将 placesClient 分配给 GMSPlacesClient 类型,而是将其分配给 sharedClient 类型方法的结果:

let placesClient = GMSPlacesClient.sharedClient()

placesClient 现在将持有 GMSPlacesClient 实例 (共享实例,稍后调用 sharedClient 将 return相同的实例,如果它没有被垃圾收集),而不是像你的原始代码那样持有 type