简体   繁体   English

数组快速行为

[英]Array of arrays swift behaviour

So im messing around with swift arrays, and they seem to be quite missleading in some areas although most of the stuff was fixed in xcode 6.1 因此,我忙于快速数组,尽管大多数内容已在xcode 6.1中修复,但它们似乎在某些领域误导了人们

i wanna make an array of arrays but it need to store the references, not only the values. 我想制作一个数组数组,但它需要存储引用,而不仅仅是值。 how can i store the references of the arrays so that updates will also take effect on the "outer array"? 我如何存储阵列的引用,以便更新也将在“外部阵列”上生效?

var a1 = [1,2]

var allarrays = [[Int]]()

allarrays.append(a1)

a1.append(99)

allarrays

allarys still gives me me [[1,2]] here, instead of [[1,2,99]] allarys仍然给了我[[1,2]],而不是[[1,2,99]]

Unfortunately (for your specific problem) swift array are value types, and as such they are always passed by value. 不幸的是(针对您的特定问题),快速数组是值类型,因此它们始终按值传递。

A workaround that might work (with inherent drawbacks) is to use NSMutableArray , which is a reference type (ie a class) instead: 一种可行的解决方法(具有固有的缺点)是使用NSMutableArray ,它是引用类型(即类),而不是:

var a1: NSMutableArray = [1,2]

var allarrays: NSMutableArray = NSMutableArray()

allarrays.addObject(a1)

a1.addObject(99)

allarrays // [[1, 2, 99]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM