Created
June 24, 2020 04:01
-
-
Save Wassmd/e4d0cf099368695e0d3219633aeeeb14 to your computer and use it in GitHub Desktop.
Problem with Unowned in closure
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 characters
import UIKit | |
class UnownedDemo { | |
func takeClosure(completion: @escaping () -> Void) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
completion() | |
} | |
} | |
} | |
class Test { | |
let unownedDemo = UnownedDemo() | |
func testClosure() { | |
unownedDemo.takeClosure { [unowned self] in | |
// unownedDemo.takeClosure { [weak self] in | |
self?.callHeMan() | |
} | |
} | |
private func callHeMan() { | |
print("Hello He Man") | |
} | |
} | |
class Test2 { | |
var test: Test? = Test() | |
func disposeTest() { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | |
print("disposed...") | |
// self.test = nil | |
} | |
} | |
func call() { | |
print("Calling...") | |
test?.testClosure() | |
} | |
func callWithDispose() { | |
print("callWithDispose...") | |
test?.testClosure() | |
disposeTest() | |
} | |
} | |
let test2 = Test2() | |
//test2.call() | |
test2.callWithDispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment