转义闭包捕获变异的“自我”参数SWIFT 5

问题描述

我正在尝试使用@propertyWrapper创建线程安全的结构,但是在设置值时,我在操场上遇到了这样的错误。 仅当我将async更改为sync时才出错,但是我只需要具有async功能

@propertyWrapper
struct SafeThread<Value>{

    private let queue = dispatchQueue(label: "sellQueue",attributes:.concurrent )
    private var value: Value
    
    
    init (wrappedValue: Value){
        self.value = wrappedValue;
    }
    
    
    var wrappedValue :Value {
        get { queue.sync { value } }
        set { queue.async(flags: .barrier) { self.value = newValue } } // there an error 
        
    }
}

我想在哪里使用它:

class Safe{
@SafeThread var foo = 0;
    
func threadSafetyExperiment() {
    
    dispatchQueue.global().sync {

        dispatchQueue.concurrentPerform(iterations: 1_000) { _ in
            self.foo += 1
        }
        print(self.foo)
    }
}
}

解决方法

您需要使用nonmutating set并为此使用外部存储来存储包装的值。

这是可行的方法。在Xcode 12 / iOS 14上进行了测试。

@propertyWrapper
struct SafeThread<Value>{

    private class Storage {
        var value: Value
        init(initialValue: Value) {
            value = initialValue
        }
    }
    private let queue = DispatchQueue(label: "sellQueue",attributes:.concurrent )
    
    private var storage: Storage
    init (wrappedValue: Value){
        self.storage = Storage(initialValue: wrappedValue);
    }
    
    var wrappedValue :Value {
        get { queue.sync { self.storage.value } }
        nonmutating set { queue.async(flags: .barrier) { self.storage.value = newValue } }
    }
}
,

这是一个重复的问题。只需将SafeThread设为一个班级即可。