Created
March 17, 2018 14:51
-
-
Save lukevanin/02f42f7ea228d35fcf1a273196079b1d to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// Example using self.map to get a strong reference to a weak reference on self. | |
// | |
import Foundation | |
import PlaygroundSupport | |
final class Test { | |
private let queue: DispatchQueue | |
private var items: [Int] | |
init(queue: DispatchQueue, items: [Int]) { | |
self.queue = queue | |
self.items = items | |
} | |
func test() { | |
queue.async { [weak self] in | |
self.map { `self` in | |
let sum = self.items.reduce(0, +) | |
print("Sum is \(sum)") | |
} | |
} | |
} | |
} | |
let t = Test( | |
queue: .global(), | |
items: Array(1 ... 100) | |
) | |
t.test() | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment