I have this struct called Address where I would pass down a CLPlacemark as a parameter. The problem here is I couldn't get the Region or Division from existing iOS frameworks and classes.
struct Address: Codable {
var street: String?
var city: String?
var state: String?
var zip_code: String?
var formatted_address: String?
var latitude: Double?
var longitude: Double?
var region: String?
var division: String?
init?(placemark: CLPlacemark) {
var address = ""
if let address1 = placemark.thoroughfare {
self.street = address1
address += " \(address1),"
}
if let address2 = placemark.subThoroughfare {
address += " \(address2),"
self.street = address2
}
if let city = placemark.locality {
address += " \(city),"
self.city = city
}
if let state = placemark.administrativeArea {
address += " \(state),"
self.state = state
}
if let zip = placemark.postalCode {
address += " \(zip)"
self.zip_code = zip
}
if let location = placemark.location {
let coordinates = location.coordinate
self.latitude = coordinates.latitude
self.longitude = coordinates.longitude
}
self.formatted_address = address
}
var coordinates: CLLocationCoordinate2D? {
if let latitude = self.latitude, let longitude = self.longitude {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
return nil
}
}
How could I achieve this? I'm thinking one way is to just list down the regions and divisions using a dictionary, with the array of state and if the dictionary which holds an array contains the state then it is the region/division. Is there any better solution?
The only solution I got for this was just to declare two static arrays of
String: [String]dictionary. One is an array of regions and another is an array of divisions.It looks something like below:
Then to get the right value out of the division and region. I would use the state that I get from the placemark.
I couldn't find a better solution as of the moment, but if you guys have any suggestions I would gladly want to know.