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

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

问题描述

如何在 Swift 中创建不可变数组?

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好多少.

如果我想要不变性,最好的选择是将它包装在 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-07-26):

这个问题可以追溯到 Swift 的早期.从那时起,Swift 已经更新,因此不可变数组实际上是不可变的,如下面的答案所示.

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.

推荐答案

这在 Xcode 6 beta 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:

来自 Xcode 6 beta 3 发行说明:

From the Xcode 6 beta 3 release notes:

• Swift 中的数组已完全重新设计,具有完整的值语义,就像 Swift 中的 Dictionary 和 String 一样.这解决了各种可变性问题——现在let"数组完全不可变,var"数组完全可变——与字典和字符串正确组合,并解决其他更深层次的问题.如果您习惯于 NSArray 或 C 数组,值语义可能会令人惊讶:数组的副本现在使用高效的延迟复制实现生成所有元素的完整且独立的副本.这是 Array 的一个重大变化,还有一些性能问题需要解决.请! 查看 Swift 编程语言了解更多信息.(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)

Swift 书中有关数组的原始信息已于 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.)

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

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