在我的Cocoa应用程序中,属性在不应该更改时会发生变化。为了找出它发生的位置,我放置了有助于缩小问题范围的断言(我确实尝试过“监视变量”,但无法理解它是如何使用的)。但是现在我面临一个似乎与它应该做的相反的断言(见截图)。
我正在使用自定义运算符(见下文)让代码在另一个线程中运行。
我错过了什么,我该怎样做才能继续前进?
import Foundation
infix operator ~>
/**
Executes the lefthand closure on a background thread and,
upon completion, the righthand closure on the main thread.
Passes the background closure's output, if any, to the main closure.
*/
func ~> <R> (
backgroundClosure: @escaping () -> R,
mainClosure: @escaping (_ result: R) -> ())
{
queue.async {
let result = backgroundClosure()
DispatchQueue.main.async {
mainClosure(result)
}
}
}
private let queue = DispatchQueue(label: "Edk4ServerRequests")
自定义运算符定义。 (代码改编自https://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/)