클로저는 캡쳐한다 -> Closure는 값을 캡쳐할 때 value/reference 타입에 관계 없이 Reference Capture 한다. == strong으로 캡쳐한다 -> reference count 증가 -> (아래코드) cat 인스턴스를 할당할 때 이미 count가 올라간 상황 -> self 를 참조하는 closure을 실행한 후 cat1 = nil 해도 deinit 되지않는다 -> 메모리에서 해지 되지않음(강한 순환 참조 발생).
hacking with swift에서 좋은표현이 있다: Strong capturing
Unless you ask for something special, Swift uses strong capturing. This means the closure will capture any external values that are used inside the closure, and make sure they never get destroyed.
catfavorite을 호출하지 않으면 deinit이 된다. lazy가 아니면 self를 참조할 수 없다.
강한참조를 벗어나보자..
[weak self]를 이용!
성공!
<추가>
상식: 캡쳐리스트는 value 타입 값을 클로저 내부에서 사용할 때 사용하곤 한다.
다음사진은 클로저내부에서 self를 참조하여 인스턴스 == nil 해도 deinit 되지 않아, closure 와 인스턴스 둘다 제거하여 deinit하는 모습이다.
참고:
hacking with.swift.
http://minsone.github.io/mac/ios/swift-automatic-reference-counting-summary