Why specify [unowned self] in blocks where you depend on self being there?

后端 未结 5 1516
后悔当初
后悔当初 2021-01-19 01:37

I want self to be non-nil and I\'m sure it will be, during the blocks execution. So why explicitly specify [unowned self] ?

object.executeBlock {
    date =         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 02:36

    I answered this elsewhere. Here's the Cliff notes:

    If self could be nil in the closure use [weak self].

    If self will never be nil in the closure use [unowned self].

    If it's crashing when you use [unowned self] I would guess that self is nil at some point in that closure, which is why you had to go with [weak self] instead.

    I really liked the whole section from the manual on using strong, weak, and unowned in closures:

    https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/AutomaticReferenceCounting.html

提交回复
热议问题