0

Can anyone share some wisdom about the most conventional coding style to handle weak self in nested closure? For example:

parentClosure { [weak self] in
  // ... assuming there is no use of self here
  nestedClosure { [weak self] in
    // ...
    self?.doSomething()
  }
}

The above for sure works I think. My question is do I have to declare unowned or weak in both parent and nested closures? Is it ok just do it in parent or child, only one of them?

Sean
  • 2,967
  • 2
  • 29
  • 39
  • 1
    `weak self` - sure, but what are you trying to achieve with `unowned self`? Either you want `self` to be guaranteed (and hence use strong self), or you want someone to be able to evict `self` and so use `weak self`. But saying: "I want an non-optional self, but self can be evicted..." is a sure way to get a crash - IMO. – timbre timbre Feb 20 '23 at 20:32
  • 1
    Check this: https://stackoverflow.com/q/38739129/1187415 – Martin R Feb 20 '23 at 20:33

0 Answers0