同时访问0x10f10df48,但修改需要独占访问

时间:2017-09-26 09:41:27

标签: swift4 xcode9

我正在进行swift4迁移,并出现此警告(这使我的应用程序非常慢)。

  

同时访问0x10f10df48,但修改需要独占访问。

在线

else if (context == &KVOContext && keyPath == contentSizeKeyPath && object as? UIScrollView == scrollView) {

找不到如何解决它。

1 个答案:

答案 0 :(得分:1)

https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md 在此链接中,您可以看到& KVOContext

的冲突 像这样:

// CONFLICT.  Passing 'x' as an inout argument is a write access for the
// duration of the call.  Passing the same variable twice means performing
// two overlapping write accesses to that variable, which therefore conflict.
swap(&x, &x)

extension Int {
  mutating func assignResultOf(_ function: () -> Int) {
    self = function()  
  }
} 

我通过

解决了这个问题
struct Pair {
  var x: Int
  var y: Int
}

class Paired {
  var pair = Pair(x: 0, y: 0)
}

let object = Paired()
swap(&object.pair.x, &object.pair.y)

我的代码是

    class Paired {
        var pair = Pair(x : "PullToRefreshKVOContext")
    }

    let objects = Paired()
    override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if (context == &objects.pair.x && keyPath == contentOffsetKeyPath && object as? UIScrollView == scrollView) {
...
}