4

In my FirstViewController I have a button directing to my SecondViewController, passing data to a property in the SecondViewController. This property has a property observer, creating a new instance of the SecondViewController when set.

While it's working as I want, I wonder why it's not getting stuck in an infinite loop, creating an instance of the SecondViewController forever. And is it good practice to do it this way?

FirstViewController:

class FirstViewController: UIViewController {
    @IBAction func something(sender: UIButton) {
        let destination = storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as SecondViewController
        destination.selected = 1
        showViewController(destination, sender: self)
    }
}

SecondViewController:

class SecondViewController: UIViewController {
    var selected: Int = 0 {
        didSet {
            let destination = storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as SecondViewController
            destination.selected = selected
            showViewController(destination, sender: self)
        }
    }

    @IBAction func something(sender: UIButton) {
        selected = 2
    }
}
Henny Lee
  • 2,970
  • 3
  • 20
  • 37
  • Intuitively, it should be, and it used to produce an infinite loop, but in recent changes to Swift Apple "fixed" that. – Dániel Nagy Mar 31 '15 at 07:52
  • **It is a bug**. Click here: http://stackoverflow.com/questions/42144752/inner-didset-protection-bizarrely-extends-to-the-whole-class – Fattie May 02 '17 at 10:59

1 Answers1

2

If you check Apple's documentation for Swift in The Swift Programming Language - Properties, Apple says that:

Note:

If you assign a value to a property within its own didSet observer, the new value that you assign will replace the one that was just set.

So if you put a breakpoint in the first line of your didSet block, I believe it should only be called once.

Community
  • 1
  • 1
Edgar
  • 2,500
  • 19
  • 31
  • Thanks, did some testing and it's still a bit weird like `showViewController` in SecondViewController gets called, but it's not executed. – Henny Lee Mar 31 '15 at 08:09
  • Yeah, The fact that your code is instantiating `secondViewController` twice is weird, firstly in `something` in `FirstViewController` and secondly in `didSet` in `SecondViewController`, are you sure that's what you want? You shouldn't need to instantiate and show the view controller twice. – Edgar Mar 31 '15 at 08:50
  • That's because I'm using the `SecondViewController` to show data based on `selected`. Basically, the user selects something in `FirstViewController` and gets the `SecondViewController` with the data (based on `selected`). But if the user decides to get other data, `selected` can be changed and the new data will be shown. Also because there are some other calculations and a new toolbar with a dynamic amount of items I've chosen to just create a new instance of `SecondViewController` instead of reloading the table. – Henny Lee Mar 31 '15 at 09:07
  • For anyone googling here, it's a known (very serious) bug - http://stackoverflow.com/questions/42144752/inner-didset-protection-bizarrely-extends-to-the-whole-class – Fattie May 02 '17 at 10:59