I'm attempting to create a constant that depends on another in the following way:
class Thingy {
let paddingConstant = 13.0
let paddingDict = ["padding": paddingConstant]
}
The bottom line gives me an error "Thingy.Type does not have a member named 'paddingConstant'"
.
Is it possible to declare a constant that depends on another?
Another solution is to declare this variable lazy
. The advantage is that, unlike a calculated property, it does not perform the calculation every time you fetch its value; but the downside is that it cannot be a constant, unfortunately:
class Thingy {
let paddingConstant = 13.0
lazy var paddingDict : [String:Double] =
["padding": self.paddingConstant]
}
I regard that limitation as a bug in the Swift language.
You can move paddingDict to the init:
class Thingy {
let paddingConstant = 13.0
let paddingDict : [String: Double]
init() {
paddingDict = ["padding": paddingConstant]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With