你如何创建斯威夫特一个不可变的数组? [英] How do you create an immutable array in Swift?

查看:151
本文介绍了你如何创建斯威夫特一个不可变的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建一个斯威夫特不变阵列?

How do I create an immutable array in Swift?

的文档的粗读会建议你可以做

A superficial reading of the docs would suggest you can just do

let myArray = [1,2,3]

但可悲的是这实际上会产生一个可变的,固定大小的数组。这种可变性创建了没有料到的混淆和变异及其参数的函数通常的困惑:

But sadly this actually produces a mutable, fixed-size array. This mutability creates the usual puzzles with unsuspected aliasing and functions mutating their arguments:

let outterArray = [myArray, myArray]
outterArray[0][0] = 2000
outterArray //=> [[2000,2,3],[2000,2,3]]   surprise!

func notReallyPure(arr:Int[]) -> () { arr[0] = 3000 }
notReallyPure(myArray)
myArray // => [3000,2,3]

比C也好不了多少。

Not much better than C.

如果我想要更改的,是最好的选择真的把它包起来在的NSArray 像这样:

If I want immutability, is the best option really to wrap it in an NSArray like so:

let immutableArray = NSArray(myArray: [1,2,3])

这似乎是疯了。缺少什么我在这里?

That seems nuts. What am I missing here?

更新(2015年7月26日):

这个问题从斯威夫特的最早期的日期。雨燕自那时以来已更新,这样一成不变的数组实际上是不可改变的,如下面的回答表示。

This question dates from the very early days of Swift. Swift has since then been updated so that immutable arrays are actually immutable, as answers below indicate.

推荐答案

这已经与X code 6测试版3.改变,而以前是半可变的,象你所说的,用自己的元素多变,但其长度数组固定的,一成不变的,现在阵列共享相同的值语义字典:

This has changed with Xcode 6 beta 3. While arrays used to be semi-mutable, as you describe, with their elements changeable but their length fixed, now immutable arrays share the same value semantics as Dictionaries:

从X code 6的Beta 3版本说明:

From the Xcode 6 beta 3 release notes:

•斯威夫特阵列已经完全重新设计具有完全值语义像字典和字符串一直都在斯威夫特。这解决各种问题的可变性 - 现在是一个'让'阵完全是不可变的,而变种数组是完全可变的 - 用字典和字符串组成恰当,解决了其他更深层的问题。如果你正在使用的NSArray或C数组值语义可能会令人吃惊:数组的一个副本现在生产使用高效的懒惰复制实现所有元素的完整和独立的副本。这是为数组的重大改变,以及还存在需要解决的一些性能问题。请
  !更多信息请参见雨燕编程语言。 (17192555)

• Array in Swift has been completely redesigned to have full value semantics like Dictionary and String have always had in Swift. This resolves various mutability problems – now a 'let' array is completely immutable, and a 'var' array is completely mutable – composes properly with Dictionary and String, and solves other deeper problems. Value semantics may be surprising if you are used to NSArray or C arrays: a copy of the array now produces a full and independent copy of all of the elements using an efficient lazy copy implementation. This is a major change for Array, and there are still some performance issues to be addressed. Please !see the Swift Programming Language for more information. (17192555)

在雨燕本书阵列的原始信息进行了更新于2014年7月7日以反映的Beta 3的变化。 (如果你使用的是Mac上的iBooks,因为我是,你可能需要删除并重新下载它拿起7月7日更新,我无法得到自动更新的东西。)

The original information on arrays in the Swift book was updated on 7th July 2014 to reflect the beta 3 changes. (If you're using iBooks on a Mac, as I was, you may need to delete and re-download it to pick up the 7th July update—I couldn't get the thing to update automatically.)

这篇关于你如何创建斯威夫特一个不可变的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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