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

iPe*_*ter 2 arrays ios swift

我经历了这个 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) 
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

Bha*_*ara 6

您应该尝试更新此行

createGameModel?.createGameOptions?.removeSubrange(2...((createGameModel?.createGameOptions?.count)! - 2))
Run Code Online (Sandbox Code Playgroud)

let count = (createGameModel?.createGameOptions?.count)!
createGameModel?.createGameOptions?.removeSubrange(2...(count - 2))
Run Code Online (Sandbox Code Playgroud)

尝试分享结果


Sha*_*ikh 5

在 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
}
Run Code Online (Sandbox Code Playgroud)

有关此内容的更多详细信息,请查看这篇文章。 独占访问错误