I’m in the process of getting my head around swift and have been playing around with struct. The following is a cut down version of a struct that works. However, I appear to have used variable names (azimuth & inclination) in a “didSet” construct before its declaration further down in the struct. Why does this work and not raise an error? I can also rearrange the code such that the variables inclination and azimuth are declared above the variables inclinationError and azimuthError and the code works fine, however, there is still the apparent problem of variable names being used before declaration, in this case “azimuthError” and “inclinationError”. Why is the compiler not yelling at me? I’m curious to why I can get away with this.
Example code:
struct PlaneOrientation {
// Structure for the orientation of a geological plane.
let inclinationRange = -90.0…90.0
let azimuthRange = 0.0…360.0
var inclinationError = false {
didSet {
if !(inclinationRange ~= inclination) { inclinationError = oldValue } //////// This is where “inclination” is first used but has not been declared
}
}
var azimuthError = false {
didSet {
if !(azimuthRange ~= azimuth) { azimuthError = oldValue } //////// This is where “azimuth” is first used but has not been declared
}
}
var inclination: Double { // Maximum angle (degrees) of inclination of a plane measured in a vertical plane from the horizontal.
willSet {
if newValue == 0.0 { azimuth = 0.0 }
inclinationError = !(inclinationRange ~= newValue)
}
}
var azimuth: Double { // Bearing of the line of inclination of the plane measured clockwise from North (degrees) in the horizontal.
didSet {
if inclination == 0.0 { azimuth = oldValue }
}
willSet {
azimuthError = !(azimuthRange ~= newValue)
}
}
// Initialisers
init(inclination: Double, azimuth: Double) { // inclination = dip of the plane, azimuth = direction of dip.
self.inclination = inclination
self.azimuth = (inclination == 0.0 ? 0.0 : azimuth)
inclinationError = !(inclinationRange ~= inclination)
azimuthError = !(azimuthRange ~= azimuth)
}
// A few more inits in here
// Getters and Setters in here
// Some local functions in here
}