同时访问<地址>,但修改需要独占访问 [英] Simultaneous accesses to &lt;Address&gt;, but modification requires exclusive access

查看:50
本文介绍了同时访问<地址>,但修改需要独占访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在函数 myFunc 中指定用于操作的数组属性,但出现此错误.这是我的代码草图.

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 文档中有非常详尽的解释:

Very thoroughly explained in the Swift documentation:

https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html

基本上,您威胁要同时以两种不同的方式对同一对象进行变异/写入.这是不连贯的,如果编译器不阻止你,运行时就会.

Basically you are threatening to mutate / write to the same object in two different ways simultaneously. That’s incoherent and if the compiler doesn’t stop you the runtime will.

这篇关于同时访问<地址>,但修改需要独占访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆