99// * Arrays are type-safe and always clear about what they contain.
1010//
1111// * Arrays are value types, but Swift is smart about only copying when necessary to improve
12- // performance. This has important implications for immutability.
12+ // performance.
1313//
14- // * Immutable arrays (constant arrays) can allow their contents to change, which differs from the
15- // other type of Collection, Dictionaries.
14+ // * Immutable arrays are immutable in terms of the array itself and the contents of the array.
15+ // This means you can't add/remove an element nor can you modify an element of an immutable
16+ // array.
1617// ------------------------------------------------------------------------------------------------
1718
1819// Create an array of Strings
1920var someArray = Array < String > ( )
2021
2122// Shorter, more common way to define an array of Strings
22- var shorter : String [ ]
23+ var shorter : [ String ]
2324
2425// This is an array literal. Since all members are of type String, this will create a String array.
2526//
@@ -28,7 +29,7 @@ var shorter: String[]
2829[ " Eggs " , " Milk " ]
2930
3031// Let's create an array with some stuff in it. We'll use an explicit String type:
31- var commonPets : String [ ] = [ " Cats " , " Dogs " ]
32+ var commonPets : [ String ] = [ " Cats " , " Dogs " ]
3233
3334// We can also let Swift infer the type of the Array based on the type of the initializer members.
3435//
@@ -71,7 +72,7 @@ shoppingList[0] = "Six Eggs"
7172shoppingList [ 4 ... 6 ] = [ " Banannas " , " Apples " ]
7273
7374// Or we can replace two items with three, inserting a new item:
74- shoppingList [ 4 . . 6 ] = [ " Limes " , " Mint leaves " , " Sugar " ]
75+ shoppingList [ 4 ..< 6 ] = [ " Limes " , " Mint leaves " , " Sugar " ]
7576
7677// We can insert an item at a given index
7778shoppingList. insert ( " Maple Syrup " , atIndex: 3 )
@@ -102,7 +103,7 @@ for (index, value) in enumerate(shoppingList)
102103//
103104// Earlier, we saw how to declare an array of a given type. Here, we see how to declare an array
104105// type and then assign it to a stored value, which gets its type by inference:
105- var someInts = Int [ ] ( )
106+ var someInts = [ Int ] ( )
106107
107108// Add the number '3' to the array
108109someInts. append ( 3 )
@@ -113,7 +114,7 @@ someInts
113114someInts = [ ]
114115
115116// We can initialize an array and and fill it with default values
116- var threeDoubles = Double [ ] ( count: 3 , repeatedValue: 3.3 )
117+ var threeDoubles = [ Double ] ( count: 3 , repeatedValue: 3.3 )
117118
118119// We can also use the Array initializer to fill it with default values. Note that we don't need to
119120// specify type since it is inferred:
@@ -126,10 +127,11 @@ let immutableArray = ["a", "b"]
126127// separately. Therefore, you can change the contents of an immutable array, but you can't change
127128// the array itself.
128129//
129- // We change the contents of an immutable array:
130- immutableArray [ 0 ] = " b "
131-
132- // But if you try to change the size or add an element, you will get a compiler error:
130+ // We can't change the contents of an immutable array:
131+ //
132+ // immutableArray[0] = "b"
133+ //
134+ // Nor can we change the size or add an element, you will get a compiler error:
133135//
134136// immutableArray += "c"
135137
@@ -139,59 +141,43 @@ immutableArray[0] = "b"
139141// Arrays are value types that only copy when necessary, which is only when the array itself
140142// changes (not the contents.)
141143//
142- // Here are three copies of the same array:
144+ // Here are three copies of an array:
143145var a = [ 1 , 2 , 3 ]
144146var b = a
145147var c = a
146148
147- // They are all the same...
148- a [ 0 ]
149- b [ 0 ]
150- c [ 0 ]
149+ // Swift uses an efficient lazy copy to manage memory for arrays. So for the time being, a, b & c
150+ // all reference the same memory. (Note the use of '===' for testing if the objects are the same
151+ // instance.)
152+ if a === b { " a and b share the same elements " }
153+ if b === c { " b and c share the same elements " }
154+ if c === a { " c and a share the same elements " }
151155
152- // Change one value within the array and they all change:
156+ // However, if we change the contents of one array (mutating it), then it is copied and becomes its
157+ // own unique entity:
153158a [ 0 ] = 42
154159b [ 0 ]
155160c [ 0 ]
156161
157- // But if we mofify the array's size, then the array being mutated is copied and becomes its own
158- // unique entity.
159- a. append ( 4 )
160- a [ 0 ] = 1
161-
162- // Now, a is different from b and c
163- a
164- b
165- c
162+ // Now that we've changed a, it should have been copied to its own instance. Let's double-check
163+ // that only b & c are the same:
164+ if a === b { " a and b share the same elements " }
165+ if b === c { " b and c share the same elements " }
166+ if c === a { " c and a share the same elements " }
166167
167- // Since 'b' and 'c' effectivly share the same contents, you can force one to become unique without
168- // modifying the array's size using the Array's unshare() method.
169- //
170- // The unshare() method is performant because it doesn't actually copy the array contents until
171- // it has to (if ever.)
172- b. unshare ( )
168+ // The same is true if we mutate the array in other ways (mofify the array's size)...
169+ b. append ( 4 )
173170
174- // They still appear to be the same...
175- b
176- c
171+ // And now they should all be unique...
172+ if a === b { " a and b share the same elements " }
173+ if b === c { " b and c share the same elements " }
174+ if c === a { " c and a share the same elements " }
177175
178- // ...but b is actually a unique copy. Let's change an element in b:
179- b [ 0 ] = 99
180-
181- // And we can see that our change only affects b:
176+ // Now, we have three different arrays...
177+ a
182178b
183179c
184180
185- // We can further verify this by comparing if they are the same instance:
186- if b === c
187- {
188- " b & c still share same array elements "
189- }
190- else
191- {
192- " b & c now refer to two independent arrays "
193- }
194-
195181// This works with sub-arrays, too:
196182if b [ 0 ... 1 ] === b [ 0 ... 1 ]
197183{
@@ -201,12 +187,3 @@ else
201187{
202188 " these guys are NOT shared "
203189}
204-
205- // Forcing a copy of an array
206- //
207- // Use the copy() method to force a shallow copy.
208- //
209- // Unlike the unshare method, the copy will happen immediately when calling copy().
210- var d = a. copy ( )
211- a [ 0 ] = 101
212- d [ 0 ]
0 commit comments