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

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

问题描述

我阅读文档,我不断地在一些语言的设计决策摇头。但是,这真的让我不解的是阵列的处理方式。

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

下面 A B 都是 [1,42,3] ,我可以接受。数组引用 - OK!

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

电子 [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: Arrays have changed and now have value semantics. Much more sane!

推荐答案

注意数组语法和语义以x code测试改为3版(适用的blog帖子),因此,问题不再适用。适用于测试版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-般的性能)。引述语<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_112\">book:

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.

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

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