How to extract street, city, etc. from GMSPlace Address Components

后端 未结 10 1692
栀梦
栀梦 2021-02-20 02:51

I\'m using Google Places API for iOS and can successfully retrieve nearby places and present the address as a string. What I\'m trying to do is extract address components such

10条回答
  •  终归单人心
    2021-02-20 03:44

    A safe Swift 4.2 solution:

    let name = place.addressComponents?.first(where: { $0.type == "city" })?.name
    

    selectedPlace.addressComponents is a array of GMSAddressComponent which have 2 properties type and name. In you case it will be like

        for component in addressComponents! {
        if component.type == "city" {
        print(component.name)
        }
       }
    

    GMSAddressComponent is a class not a dictionary or array that's you are getting this error.

    Additional component types can be referred from the link.

提交回复
热议问题