6

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.

1
  • 2
    it doesn't make sense to non-readable property. just use method SetSelectedIndex(Int) Commented Jun 16, 2014 at 5:51

1 Answer 1

25

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

var selectedIndex: Int {
  didSet {
    selectItemAtIndex(selectedIndex)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.