IS
if let first = (array.first { $0.id = “1” }) { ... }
Size: a a a
IS
if let first = (array.first { $0.id = “1” }) { ... }
В
НС
🚧
BS
BS
BS
🚧
BS
BS
S
S
IS
import Foundation
import PlaygroundSupport
PlaygroundSupport.PlaygroundPage.current.needsIndefiniteExecution = true
// initial conditions
let delays = [1, 2, 1, 3, 1]
let callback: (_ index: Int) -> Void = { index in
print("callback \(index), \(Date())")
}
// collect timings
var offset = 0
let times: [Int] = delays.map { offset += $0; return offset }
print(times)
let queue = DispatchQueue(label: "queue")
let now = DispatchTime.now()
print("start \(Date())")
// make tasks
var workItems: [DispatchWorkItem] = []
times.enumerated().forEach { item in
let workItem = DispatchWorkItem { callback(item.offset) }
workItems.append(workItem)
queue.asyncAfter(deadline: now + TimeInterval(item.element), execute: workItem)
}
// cancel
let timeout: TimeInterval = 4.0
DispatchQueue.global().asyncAfter(deadline: now + timeout) {
workItems.forEach { $0.cancel() }
}
SV
IS
🚧
SV
MB
MB