A standard implementation of a computed variable with a getter and installer is as follows:
private var stored: String public var computed: String { get { return stored } set { stored = newValue } }
The name newValue
in the setter definition represents the value that applies.
You can drop the following on the playground to see:
struct SomeStruct { private var stored: String = "" public var computed: String { get { return self.stored } set { self.stored = newValue } } } var value = SomeStruct() print(value.computed) value.computed = "new string" print(value.computed)
If you really wanted to reference getter in your setter definition, you could. Perhaps you would like to apply only newValue
in the installer based on some getter condition:
public var computed: String { get { return self.stored } set { self.stored = self.computed == "" ? self.computed : newValue } }
It is also relevant and should meet your requirements.
source share