Basically, I want to do like this:
var counter : Int = 0;
private var _data : String;
var data : String {
get { counter += 1; return _data; }
set { _data = newValue; }
}
Then I want to reduce it like this:
var counter : Int = 0;
var data : String {
get { counter += 1; return data; }
set { data = newValue; }
}
But I noticed that this can't be done. (Error: Variable used within its initial value). So I then want to simplify it like this:
var counter : Int = 0;
var data : String {
didGet { counter += 1; }
}
But there's no such thing as didGet
. Is there any way to do this without adding new other variable? I need to run counter += 1
every time data
is accessed, without adding new variable as storage. Thanks.
No, there is no way to do it without adding new variable.
If you return in the get
method of the variable the same variable, you will create infinite loop.
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