How do you implement setter without getter in Swift? I need to call a method when the value is set:
var selectedIndex : Int{
set {
selectItemAtIndex(newValue)
}
}
but in Swift, you are required to use both getter and setter, not just one.
You can use the property observer didSet
. This will be called immediately after setting the value.
var selectedIndex: Int {
didSet {
selectItemAtIndex(selectedIndex)
}
}
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