Last active
May 15, 2020 08:21
-
-
Save 4np/fd6981df487f5ed42426a9f07c1db32f to your computer and use it in GitHub Desktop.
Revisions
-
4np revised this gist
May 15, 2020 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,6 +20,7 @@ class TestClass { print("Inside block 1: \(String(describing: self?.retainCount))") self?.block2 = { // `self` is still weekly captured from `block1`. print("Inside block 2: \(String(describing: self?.retainCount))") } @@ -39,12 +40,12 @@ class TestClass { print("Starting Bar: \(retainCount)") block1 = { [weak self] in // Unwrapping `self`. guard let self = self else { return } print("Inside block 1: \(self.retainCount)") // As you now have a strong reference to `self`, you to again weekly capture `self` in nested closures. self.block2 = { [weak self] in print("Inside block 2: \(String(describing: self?.retainCount))") } -
4np revised this gist
May 15, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,10 +39,12 @@ class TestClass { print("Starting Bar: \(retainCount)") block1 = { [weak self] in // Unwrapping `self`... guard let self = self else { return } print("Inside block 1: \(self.retainCount)") // ...requires you to again weekly capture `self` in nested closures. self.block2 = { [weak self] in print("Inside block 2: \(String(describing: self?.retainCount))") } -
4np revised this gist
May 15, 2020 . No changes.There are no files selected for viewing
-
4np created this gist
May 15, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ import Cocoa class TestClass { var name = "" var block1: (() -> Void)? var block2: (() -> Void)? var retainCount: Int { CFGetRetainCount(self) } init() { print("Initializing: \(retainCount)") } func foo() { print("Starting Foo: \(retainCount)") block1 = { [weak self] in print("Inside block 1: \(String(describing: self?.retainCount))") self?.block2 = { print("Inside block 2: \(String(describing: self?.retainCount))") } // Execute block 2 self?.block2?() print("Finishing block 1: \(String(describing: self?.retainCount))") } // Execute block 1 block1?() print("Finishing Foo: \(retainCount)") } func bar() { print("Starting Bar: \(retainCount)") block1 = { [weak self] in guard let self = self else { return } print("Inside block 1: \(self.retainCount)") self.block2 = { [weak self] in print("Inside block 2: \(String(describing: self?.retainCount))") } // Execute block 2 self.block2?() print("Finishing block 1: \(self.retainCount)") } // Execute block 1 block1?() print("Finishing Bar: \(retainCount)") } deinit { print("Deinitializing: \(retainCount)") } } var testClass: TestClass? = TestClass() testClass?.foo() testClass?.bar() testClass = nil