# Did set

**URL:** <https://forums.swift.org/t/did-set/45440>\
**Category:** Using Swift\
**Tags:** didset\
**Created:** [March 8, 2021, 1:14pm UTC](https://forums.swift.org/t/did-set/45440 "2021-03-08T13:14:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koshub](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@Koshub](https://forums.swift.org/u/Koshub)\
**Post date:** [March 8, 2021, 1:14pm UTC](https://forums.swift.org/t/did-set/45440/1 "2021-03-08T13:14:08Z")

</div>

I tried this code in the playground and it doesn't work. What is interesting - the fixed version. I was not expected this.

```swift
class SomeController: PresenterListener {
    var presenter: Presenter? {
        didSet { presenterUpdated() }
    }
    private func presenterUpdated() {
        presenter?.listener = self
    }
}
protocol PresenterListener: class {
}
protocol Presenter {
    var listener: PresenterListener? { get set }
}
class SomePresenter: Presenter {
    weak var listener: PresenterListener?
}
let controller = SomeController()
controller.presenter = SomePresenter()
print("✅")

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [March 8, 2021, 3:34pm UTC](https://forums.swift.org/t/did-set/45440/2 "2021-03-08T15:34:27Z")

</div>

Your code has infinite recursion. The line “`presenter?.listener = self`” causes a mutation of `self.presenter`, which is modeled as first getting the value then setting it. That means it triggers the `didSet` handler, which starts the same loop over again endlessly.

---

<div class="post-metadata">

**Author:** ![Koshub](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@Koshub](https://forums.swift.org/u/Koshub)\
**Post date:** [March 8, 2021, 3:38pm UTC](https://forums.swift.org/t/did-set/45440/3 "2021-03-08T15:38:23Z")

</div>

Yep, that is recursion. Isn't it looks strange when the working code looks like this?

```swift
class SomeController: PresenterListener {
    var presenter: Presenter? {
        didSet {
            presenter?.listener = self
            presenterUpdated()
        }
    }
    
    private func presenterUpdated() {
        //...
    }
}

```
