I'm trying to create a variable of type Dictionary<String, Double>
where one of the value in the dictionary is calculated from an instance variable of the type.
class CalculatorBrain {
private var accumulator = 0.0
private var operations: Dictionary<String, Double> = [
"π": M_PI,
"√": sqrt(accumulator)
]
}
But I have encountered the following error.
Cannot use instance member 'accumulator' within property initializer; property initializers run before 'self' is available
I thought maybe this has something to do with how Swift initialisation order works, so I added keyword lazy
to the dictionary variable, so that it will only be initialised when it is used. However, this doesn't work as well.
Why does the error occur and how to make it work?
class CalculatorBrain {
private var accumulator = 0.0
lazy private var operations: Dictionary<String, Double> = [
"π": M_PI,
"√": sqrt(accumulator)
]
}