passing Google Places data to UILabel

148 Views Asked by At

I am trying to pass data collected after selecting a place from GMSAutocompleteViewController, and having some issues. I am getting this error: Could not cast value of type 'NSTaggedPointerString' (0x1afeb5c50) to 'UILabel' (0x1afedd508). Not sure what I am doing wrong...thanks for any and all help!

    // 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
    }()
2

There are 2 best solutions below

0
unixb0y On BEST ANSWER
self.placeNameLabel=UILabel()
if let name = selectedPlace as? String
{
   self.placeNameLabel.text = name
}

You have to assign the text to the UILabel's text property. A UILabel does display text but it does much more than that so it has more attributes, like: backgroundColor, size, frame, bounds, etc.
So the text part is just one of many properties that a UILabel has and you have to explicitly specify that.
Btw you can remove the first line if you created the label in a Storyboard and connected it with an outlet.

By the way:
This:

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

always returns true, so you can completely forget about it.

0
rmaddy On

There's no need for any cast (and certainly not two). Simply do:

self.placeNameLabel.text = place.name

You were trying to assign the string to a label. You need to assign the string to the label's text.

One other serious issue. You need to update the label inside the completion handler of lookUpPlaceID. As you have it now, you set the label to the place parameter passed to the view controller method but you need to use the place parameter passed to the completion handler of the lookupPlaceID method. And since updating the label is a UI action, you must set the label on the main queue using 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()

}