【问题标题】:Simultaneous accesses to 0x6040000155d8, but modification requires exclusive access同时访问0x6040000155d8,但修改需要独占访问
【发布时间】:2019-01-11 09:44:50
【问题描述】:

我已经完成了this SO 问题,并且知道这是由于同时读取和写入而发生的。但就我而言,我无法弄清楚我在哪里同时读取和写入数组。

我正在做的是在插入之前从数组中删除一个子范围。例如:

var createGameOptions = [GOCreateGameDetailsModel]()
for attribute in model.gameAttributes! {
     let model = GOCreateGameDetailsModel.init(title: attribute.attribute_name!, image: nil, value: "", imageUrl: attribute.attribute_icon)
     createGameOptions.append(model)
}
if (createGameModel?.createGameOptions?.count)! > 3 {
    createGameModel?.createGameOptions?.removeSubrange(2...((createGameModel?.createGameOptions?.count)! - 2))
}
createGameModel?.createGameOptions?.insert(contentsOf: createGameOptions, at: 2) 

我们将不胜感激。

【问题讨论】:

    标签: ios arrays swift


    【解决方案1】:

    你应该尝试更新这一行

    createGameModel?.createGameOptions?.removeSubrange(2...((createGameModel?.createGameOptions?.count)! - 2))
    

    let count = (createGameModel?.createGameOptions?.count)!
    createGameModel?.createGameOptions?.removeSubrange(2...(count - 2))
    

    尝试并分享结果

    【讨论】:

    • 非常感谢。知道问题出在哪里了。
    • 最佳答案!谢谢
    【解决方案2】:

    在 Swift 4.2 中,与独占访问相关的警告变成错误,这意味着带有这些警告的项目将永远不会在 Swift 4.2 上编译

    当修改变量的变异方法被传递一个从同一变量读取的非转义闭包时,就会出现警告。例如:

    var result = Data(count:prf.cc.digestLength)
    let count = result.count
    let status = result.withUnsafeMutableBytes {
                // can not use directly result.count inside this non escaping closure. must computed before using count
    }
    

    有关此内容的更多详细信息,请查看这篇文章。 exclusive access erros

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2018-02-21
      • 2019-10-28
      • 2018-02-17
      • 1970-01-01
      • 2018-01-07
      相关资源
      最近更新 更多