I have some structs that represent locations on the Earth that represent a simple lat/lon location (Point2D), lat/lon/height location (Point3D), and a named location such as an airport name/lat/lon/height location (Airport).
struct Point2D {
var lat: Double
var lon: Double
}
struct Point3D {
var point2D: Point2D
var height_m: Double
}
struct Airport {
var name: String
var point3D: Point3D
}
These structs are used hierarchically which works fine, but it does cause some long .dot access when getting data out of an airport instance. For example, to get the airport's latitude value is airport.point3D.point2D.lat which is not ideal.
let airportLHR1 = Airport(name: "LHR", point3D: Point3D(point2D: Point2D(lat: 51.5, lon: -0.5), height_m: 25.0))
print("Airport: \(airportLHR1.name). Lat,Lon: \(airportLHR1.point3D.point2D.lat), \(airportLHR1.point3D.point2D.lon)")
// --> Airport: LHR. Lat,Lon: 51.5, -0.5
I have refactored the airport struct to 'flatten' the property .dot access, but I feel that there may be a better way to achieve this.
struct Airport2 {
var name: String
private var _point3D: Point3D
var lat: Double { get { _point3D.point2D.lat } set { _point3D.point2D.lat = newValue } }
var lon: Double { get { _point3D.point2D.lon } set { _point3D.point2D.lon = newValue } }
var height_m: Double { get { _point3D.height_m } set { _point3D.height_m = newValue } }
init(name: String, lat: Double, lon: Double, height_m: Double) {
self.name = name
self._point3D = Point3D(point2D: Point2D(lat: lat, lon: lon), height_m: height_m)
}
}
Accessing the airport's data is now much simpler...
let airportLHR2 = Airport2(name: "LHR", lat: 51.5, lon: -0.5, height_m: 25.0)
print("Airport: \(airportLHR2.name). Lat,Lon: \(airportLHR2.lat), \(airportLHR2.lon)")
// --> Airport: LHR. Lat,Lon: 51.5, -0.5
... but is there a better way to do this?