Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift array behaviour

Tags:

arrays

swift

ios8

In Swift arrays have a special behaviour, but why here does arr1 contain two times "item 1" whereas arr2 contains it only once ?

What does defining arr1 as an instance variable change here (versus defining arr2 as a local variable) ?

Update: I'm using Xcode 6.0.1

enter image description here

like image 597
thomas.g Avatar asked Oct 07 '14 12:10

thomas.g


People also ask

Are arrays in Swift dynamic?

But arrays in Swift are dynamic: you can grow them. And it is a lot less clear whether Swift is efficient in this case.

Are arrays mutable in Swift?

If you create an array, a set, or a dictionary, and assign it to a variable, the collection that's created will be mutable. This means that you can change (or mutate) the collection after it's created by adding, removing, or changing items in the collection.

How do arrays work in Swift?

Array's have fixed types, just like any other variable in Swift. That means that every element in an array has the same type, that cannot be changed once set. All array elements must have the same type, too. In the above examples, every item has a fixed type: String, Int and Double.

Which one is faster array or set in Swift?

Difference between an Array and Set in SwiftArray is faster than set in terms of initialization.


1 Answers

I can reproduce this result reliably in Xcode 6.0.1:

Xcode 6.0.1

But not in Xcode 6.1 GM:

Xcode 6.1

Looks like it's a bug that got fixed.

As a temporary workaround, this works in Xcode 6.0.1:

Workaround

like image 136
Greg Avatar answered Sep 21 '22 18:09

Greg