Skip to content

Instantly share code, notes, and snippets.

@lukevanin
Created March 17, 2018 14:51
Show Gist options
  • Save lukevanin/02f42f7ea228d35fcf1a273196079b1d to your computer and use it in GitHub Desktop.
Save lukevanin/02f42f7ea228d35fcf1a273196079b1d to your computer and use it in GitHub Desktop.
//
// 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