我在存储Google地方信息中的地方详细信息时遇到了一些问题。我可以很好地构建应用程序,但是当我从GMSAutocompleteViewController中选择一个位置时崩溃了……
“无法将类型'GMSPlace'(0x10075bec8)的值强制转换为'NSString'(0x1afec1398)。”

我想保存所选的位置数据,然后发送该数据以在UIView和UITableView中显示。不知道我在做什么错...

    // MARK: GOOGLE AUTO COMPLETE DELEGATE

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

        // Do something with the selected place.
        // A hotel in Saigon with an attribution.
//        let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
        let placeID = GMSPlace.self

        placesClient.lookUpPlaceID((placeID as AnyObject) as! String, callback: { (place, error) -> Void in
            if let error = error {
                print("lookup place id query error: \(error.localizedDescription)")
                return
            }

            guard let place = place else {
                print("No place details for \(placeID)")
                return
            }

            print("Place name \(place.name)")
            print("Place address \(place.formattedAddress)")
            print("Place placeID \(place.placeID)")
            print("Place attributions \(place.attributions)")
        })

        self.dismiss(animated: true, completion: nil)

        setupConfirmationPopUp()

    }

最佳答案

您的代码(GMSPlace.self)正在创建对GMSPlace类型本身的引用,而不是特定实例。委托方法将提供视图控制器能够自动完成的GMSPlace实例。使用此实例,您可以从名为 placeID 的属性获取位置ID。

尝试。

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

    // Do something with the selected place.
    // A hotel in Saigon with an attribution.
    // let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
    let placeID = place.placeID

    placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
        if let error = error {
            print("lookup place id query error: \(error.localizedDescription)")
            return
        }

        guard let place = place else {
            print("No place details for \(placeID)")
            return
        }

        print("Place name \(place.name)")
        print("Place address \(place.formattedAddress)")
        print("Place placeID \(place.placeID)")
        print("Place attributions \(place.attributions)")
    })

    self.dismiss(animated: true, completion: nil)

    setupConfirmationPopUp()

}

10-05 17:50