Fried Rice
Fried Rice

Reputation: 3733

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.

Upvotes: 6

Views: 3689

Answers (1)

Anil Varghese
Anil Varghese

Reputation: 42977

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

var selectedIndex: Int {
  didSet {
    selectItemAtIndex(selectedIndex)
  }
}

Upvotes: 25

Related Questions