ios - 将 Google Places 数据传递给 UILabel

标签 ios swift casting gmsplace

我试图传递从 GMSAutocompleteViewController 中选择一个位置后收集的数据,但遇到了一些问题。我收到此错误:无法将类型“NSTaggedPointerString”(0x1afeb5c50)的值转换为“UILabel”(0x1afedd508)。不确定我做错了什么......感谢所有帮助!

    // 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 = 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)")
        })

        //
        let selectedPlace = place.name
        self.placeNameLabel = (selectedPlace as AnyObject) as! UILabel

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

    }


// label I am trying to pass the place.name to
    lazy var placeNameLabel: UILabel = {
        let placeNameLabel = UILabel()
        placeNameLabel.textColor = .black
        placeNameLabel.text = "Are you sure you want to add < Placename Placename > to places you have visited?"
        placeNameLabel.frame = CGRect(x: 50, y: 120, width: 200, height: CGFloat.greatestFiniteMagnitude) // use constraints later
        placeNameLabel.numberOfLines = 0
        placeNameLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
        placeNameLabel.sizeToFit()
        placeNameLabel.layer.zPosition = 1
        placeNameLabel.isUserInteractionEnabled = true
        return placeNameLabel
    }()

最佳答案

不需要任何转换(当然不需要两个)。简单地做:

self.placeNameLabel.text = place.name

您试图将字符串分配给标签。您需要将字符串分配给标签的文本。

另一个严重的问题。您需要更新 lookUpPlaceID 完成处理程序内的标签。正如您现在所拥有的,您将标签设置为传递给 View Controller 方法的 place 参数,但是您需要使用传递给 place 的完成处理程序的参数 lookupPlaceID 方法。由于更新标签是一个 UI 操作,您必须使用 DispatchQueue.main.async 在主队列上设置标签。

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    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)")

        DispatchQueue.main.async {
            self.placeNameLabel = place.name
        }
    })

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

}

关于ios - 将 Google Places 数据传递给 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42915624/

相关文章:

c - 是否存在整数在转换为 double 时失去精度的情况?

Java - Enumerable.Cast() 像 C#?

swift - 变量未保存在闭包之外

generics - 有理数对象的整数输入的通用构造函数

ios - 快速解压文件

ios - 为什么在使用 Codemagic 为 Flutter 构建代码时会收到 "Failed to build for iOS"?

ios - 在应用未显示内容时收到相应的推送通知时显示 Urban Airship 登陆页面

ios - 在 Firebase 上保存和检索图像

ios - JetBrains AppCode 如何启动 iOS 模拟器?

ios - MPMoviePlayerController 行为不正常