Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift implement setter without getter

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.

like image 894
Fried Rice Avatar asked Jun 16 '14 05:06

Fried Rice


1 Answers

You can use the property observer didSet. This will be called immediately after setting the value.

var selectedIndex: Int {
  didSet {
    selectItemAtIndex(selectedIndex)
  }
}
like image 144
Anil Varghese Avatar answered Oct 22 '22 04:10

Anil Varghese