Swift - Generate an Address Format from Reverse Geocoding

前端 未结 10 1404
难免孤独
难免孤独 2021-01-31 03:46

I am trying to generate a Formatted Full address using CLGeocoder in Swift 3. I referred to this SO thread to get the code given below.

However, sometimes the app crashe

10条回答
  •  抹茶落季
    2021-01-31 04:28

    Here's a 2-3 line version of the answers here:

        func getAddress(placemarks: [CLPlacemark]) -> String {
            guard let placemark = placemarks.first, !placemarks.isEmpty else {return ""}
            let outputString = [placemark.locality,
                                placemark.subLocality,
                                placemark.thoroughfare,
                                placemark.postalCode,
                                placemark.subThoroughfare,
                                placemark.country].compactMap{$0}.joined(separator: ", ")
            print(outputString)
            return outputString
        }
    

提交回复
热议问题