Swift 3.0 Error: Escaping closures can only capture inout parameters explicitly by value

后端 未结 3 1120
遇见更好的自我
遇见更好的自我 2020-11-29 22:54

I\'m trying to update my project to Swift 3.0 but I have some difficulties. I\'m getting next error: \"Escaping closures can only capture inout parameters explicitly by valu

3条回答
  •  伪装坚强ぢ
    2020-11-29 23:46

    If you want to modify a variable passed by reference in an escaping closure, you can use KeyPath. Here is an example:

    class MyClass {
        var num = 1
        func asyncIncrement(_ keyPath: WritableKeyPath) {
            DispatchQueue.main.async {
                // Use weak to avoid retain cycle
                [weak self] in
                self?[keyPath: keyPath] += 1
            }
        }
    }
    

    You can see the full example here.

提交回复
热议问题