swift4 - Simultaneous accesses to , but modification requires exclusive access
问题描述
I want to specify the property which is an array for manipulation in the function myFunc
, but I am getting this error. Here is my code sketch.
self.data = MyObject()
func myFunc(x: inout [Int], y: inout [[Int]]) {
//manipulation code to x and y
}
myFunc(x: &self.data.array1, y: &self.data.array2)
myFunc(x: &self.data.array3, y: &self.data.array4)
Any idea how to make it work? Is there a better pattern I should use for this use case? Thx for advance!
解决方案
在 Swift 文档中进行了非常彻底的解释:
https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html
基本上,您威胁要同时以两种不同的方式对同一个对象进行变异/写入。这是不连贯的,如果编译器不阻止你运行时会。
推荐阅读
- java - 基于 Java 中的标头写入 csv/excel
- html - 为什么
html 的标签不是分段内容,而是 - javascript - 如何在jquery上总计两个不同的下拉选择
- python - 应用于形状张量的 IShuffleLayer 必须具有 0 或 1 个重塑尺寸:尺寸为 [1,4]
- python - 向合并的数据框添加子索引
- ios - 只调用一次长按按钮动作而不是快速射击
- javascript - 由于路径为空,如何修改我的代码以防止应用程序崩溃
- caching - 指令是如何以及在哪里缓存的?
- c# - 如何压缩目录然后返回结果字节数组,而不在磁盘上物理创建 zip 文件?
- jquery - 如何使用 npm 从 svelte 使用 jquery 和数据表