Swift 数组赋值不一致(既不是引用也不是深拷贝)是否有原因? [英] Is there a reason that Swift array assignment is inconsistent (neither a reference nor a deep copy)?

查看:29
本文介绍了Swift 数组赋值不一致(既不是引用也不是深拷贝)是否有原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读文档,并且一直对语言的一些设计决策摇头.但真正让我感到困惑的是数组是如何处理的.

I'm reading the documentation and I am constantly shaking my head at some of the design decisions of the language. But the thing that really got me puzzled is how arrays are handled.

我冲到操场并尝试了这些.你也可以试试.所以第一个例子:

I rushed to the playground and tried these out. You can try them too. So the first example:

var a = [1, 2, 3]
var b = a
a[1] = 42
a
b

这里ab都是[1, 42, 3],我可以接受.引用了数组 - 好的!

Here a and b are both [1, 42, 3], which I can accept. Arrays are referenced - OK!

现在看这个例子:

var c = [1, 2, 3]
var d = c
c.append(42)
c
d

c[1, 2, 3, 42] 但是 d[1, 2, 3].也就是说,d 在上一个示例中看到了更改,但在本示例中没有看到.文档说那是因为长度改变了.

c is [1, 2, 3, 42] BUT d is [1, 2, 3]. That is, d saw the change in the last example but doesn't see it in this one. The documentation says that's because the length changed.

现在,这个怎么样:

var e = [1, 2, 3]
var f = e
e[0..2] = [4, 5]
e
f

e[4, 5, 3] ,很酷.多索引替换很好,但是 f 即使长度没有改变,仍然看不到变化.

e is [4, 5, 3], which is cool. It's nice to have a multi-index replacement, but f STILL doesn't see the change even though the length has not changed.

总而言之,如果您更改 1 个元素,对数组的公共引用会看到更改,但是如果您更改多个元素或附加项目,则会创建一个副本.

So to sum it up, common references to an array see changes if you change 1 element, but if you change multiple elements or append items, a copy is made.

对我来说,这似乎是一个非常糟糕的设计.我这样想对吗?有什么原因我不明白为什么数组应该像这样?

This seems like a very poor design to me. Am I right in thinking this? Is there a reason I don't see why arrays should act like this?

EDIT:数组已更改,现在具有值语义.理智多了!

EDIT: Arrays have changed and now have value semantics. Much more sane!

推荐答案

注意数组语义和语法在 Xcode beta 3 版本中发生了变化(博客文章),所以这个问题不再适用.以下答案适用于 Beta 2:

Note that array semantics and syntax was changed in Xcode beta 3 version (blog post), so the question no longer applies. The following answer applied to beta 2:

这是出于性能原因.基本上,他们尽量避免复制数组(并声称类似 C 的性能").引用语言 :

It's for performance reasons. Basically, they try to avoid copying arrays as long as they can (and claim "C-like performance"). To quote the language book:

对于数组,只有当您执行有可能修改数组长度的操作时才会进行复制.这包括追加、插入或删除项目,或使用范围下标替换数组中的一系列项目.

For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array. This includes appending, inserting, or removing items, or using a ranged subscript to replace a range of items in the array.

我同意这有点令人困惑,但至少对它的工作原理有一个清晰而简单的描述.

I agree that this is a bit confusing, but at least there is a clear and simple description of how it works.

该部分还包括有关如何确保唯一引用数组、如何强制复制数组以及如何检查两个数组是否共享存储的信息.

That section also includes information on how to make sure an array is uniquely referenced, how to force-copy arrays, and how to check whether two arrays share storage.

这篇关于Swift 数组赋值不一致(既不是引用也不是深拷贝)是否有原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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