Skip to content

Commit 6244068

Browse files
committed
Merge branch 'ChilliCoders-master'
Updated playgrounds to conform to latest Swift langauge changes that came with Xcode Beta 3.
2 parents f2f0b2d + dd5a3cb commit 6244068

File tree

17 files changed

+112
-117
lines changed

17 files changed

+112
-117
lines changed

10. Properties.playground/section-1.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DataImporter
4444
class DataManager
4545
{
4646
@lazy var importer = DataImporter()
47-
var data = String[]()
47+
var data = [String]()
4848
}
4949

5050
// Now let's instantiate the data manager and add some simple data to the class:
@@ -297,4 +297,4 @@ class SomeClass
297297

298298
// This is read-only, but you can also do read/write
299299
class var computedTypeProperty: Int { return 4 }
300-
}
300+
}

12. Subscripts.playground/section-1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct Matrix
4444
{
4545
let rows: Int
4646
let columns: Int
47-
var grid: Double[]
47+
var grid: [Double]
4848

4949
init (rows: Int, columns: Int)
5050
{

14b. Initializer Chaining.playground/section-1.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ class ClassWithPI
163163
// initialized and then returned.
164164
struct CheckerBoard
165165
{
166-
let boardColors: Bool[] =
166+
let boardColors: [Bool] =
167167
{
168-
var temporaryBoard = Bool[]()
168+
var temporaryBoard = [Bool]()
169169
var isBlack = false
170170
for i in 1...10
171171
{

18. Type Casting.playground/section-1.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ for item in library
107107
//
108108
// Let's see AnyObject in action. We'll define an array of type AnyObject[] and populate it with
109109
// some movies:
110-
let someObjects: AnyObject[] =
110+
let someObjects: [AnyObject] =
111111
[
112112
Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"),
113113
Movie(name: "Moon", director: "Duncan Jones"),
@@ -127,15 +127,15 @@ for object: AnyObject in someObjects
127127
}
128128

129129
// Alternatively, we can downcast the array itself rather than each item:
130-
var someMovies = someObjects as Movie[]
130+
var someMovies = someObjects as [Movie]
131131
for movie in someMovies
132132
{
133133
"Movie: '\(movie.name)' was directed by \(movie.director)"
134134
}
135135

136136
// Finally, we can avoid the additional local variable and performt he downcast right inside
137137
// the loop structure:
138-
for movie in someObjects as Movie[]
138+
for movie in someObjects as [Movie]
139139
{
140140
"Movie: '\(movie.name)' was directed by \(movie.director)"
141141
}
@@ -145,7 +145,7 @@ for movie in someObjects as Movie[]
145145
//
146146
// Let's see this in action. We'll create an array of type Any[] and fill it with random bits and
147147
// pieces of stuff:
148-
var things = Any[]()
148+
var things = [Any]()
149149

150150
things.append(0)
151151
things.append(0.0)

2. Basic operations.playground/section-1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var d = a % b // Floating point remainder
3636
// The range operator with two dots means up to but NOT including the final value.
3737
//
3838
// This is called the "Half-Closed Range Operator"
39-
for i in 1..10
39+
for i in 1..<10
4040
{
4141
i // prints 1 through 9
4242
}

20. Extensions.playground/section-1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension Int
108108
{
109109
func repititions(task: () -> ())
110110
{
111-
for i in 0..self
111+
for i in 0..<self
112112
{
113113
task()
114114
}

21. Protocols.playground/section-1.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ extension Hamster: TextRepresentable
235235
// Hamsters and Dice don't have much in common, but in our sample code above, they both conform
236236
// to the TextRepresentable protocol. Because of this, we can create an array of things that are
237237
// TextRepresentable which includes each:
238-
let textRepresentableThigns: TextRepresentable[] = [d6, tedTheHamster]
238+
let textRepresentableThigns: [TextRepresentable] = [d6, tedTheHamster]
239239

240240
// We can now loop through each and produce its text representation:
241241
for thing in textRepresentableThigns
@@ -341,7 +341,7 @@ class Animal
341341
}
342342

343343
// We can store our objects into an array of type AnyObject[]
344-
let objects: AnyObject[] =
344+
let objects: [AnyObject] =
345345
[
346346
Circle(radius: 3.0),
347347
Country(area: 4356947.0),

22. Generics.playground/section-1.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ bString
8181
// type for a property, the input parameter for a method and the return value for a method.
8282
struct Stack<T>
8383
{
84-
var items = T[]()
84+
var items = [T]()
8585
mutating func push(item: T)
8686
{
8787
items.append(item)
@@ -133,7 +133,7 @@ func doSomethingWithKeyValue<KeyType: Hashable, ValueType>(someKey: KeyType, som
133133
// element from the array with the value being searched for. By including the Equatable, we tell
134134
// the generic function that it is guaranteed to receive only values that meet that specific
135135
// criteria.
136-
func findIndex<T: Equatable>(array: T[], valueToFind: T) -> Int?
136+
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int?
137137
{
138138
for (index, value) in enumerate(array)
139139
{
@@ -177,7 +177,7 @@ struct StackContainer<T> : Container
177177
{
178178
// Here we find our original stack implementation, unmodified
179179

180-
var items = T[]()
180+
var items = [T]()
181181
mutating func push(item: T)
182182
{
183183
items.append(item)
@@ -251,7 +251,7 @@ func allItemsMatch
251251
}
252252

253253
// Check each pair of items to see if they are equivalent
254-
for i in 0..someContainer.count
254+
for i in 0..<someContainer.count
255255
{
256256
if someContainer[i] != anotherContainer[i]
257257
{

4a. Arrays.playground/section-1.swift

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
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
1920
var 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"
7172
shoppingList[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
7778
shoppingList.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
108109
someInts.append(3)
@@ -113,7 +114,7 @@ someInts
113114
someInts = []
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:
143145
var a = [1, 2, 3]
144146
var b = a
145147
var 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:
153158
a[0] = 42
154159
b[0]
155160
c[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
182178
b
183179
c
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:
196182
if 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]

4a. Arrays.playground/timeline.xctimeline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6385&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=424365425.423988">
6+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6335&amp;EndingColumnNumber=5&amp;EndingLineNumber=17&amp;StartingColumnNumber=4&amp;StartingLineNumber=17&amp;Timestamp=426611031.566827">
77
</LoggerValueHistoryTimelineItem>
88
</TimelineItems>
99
</Timeline>

4b. Dictionaries.playground/section-1.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
// Let's use that literal to define and initialize a Dictionary.
2020
//
2121
// In this case, we use type annotation to explicitly declare a Dictionary containing String keys
22-
// and String values:
23-
var airports: Dictionary<String, String> = ["TYO": "Tokyo", "DUB": "Dublin", "APL": "Apple Intl"]
22+
// and String values. This uses the syntactic sugar "[ KeyType: ValueType ]" to declare the
23+
// dictionary.
24+
var airports: [String : String] = ["TYO": "Tokyo", "DUB": "Dublin", "APL": "Apple Intl"]
25+
26+
// The declaration for airports above could also have been declared in this way:
27+
var players: Dictionary<String, String> = ["Who" : "First", "What" : "Second"]
2428

2529
// In the case below, the literal contains only Strings for all keys and only Strings for all
2630
// values, so type inference works in our favor allowing us to avoid the type annotation:
@@ -102,7 +106,7 @@ namesOfIntegers = [:]
102106
// An immutable dictionary is a constant.
103107
let immutableDict = ["a": "one", "b": "two"]
104108

105-
// Unlike arrays, you cannot modify the contents of an immutable dictionary. The following lines
109+
// Similar to arrays, we cannot modify the contents of an immutable dictionary. The following lines
106110
// will not compile:
107111
//
108112
// immutableDict["a"] = "b" // You cannot modify an element
@@ -119,3 +123,4 @@ copiedAges["Peter"] = 24
119123

120124
// And we can see that the original is not changed:
121125
ages["Peter"]
126+

4b. Dictionaries.playground/timeline.xctimeline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4582&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=424365435.370573">
6+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4822&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=426611538.879704">
77
</LoggerValueHistoryTimelineItem>
88
<LoggerValueHistoryTimelineItem
9-
documentLocation = "#CharacterRangeLen=11&amp;CharacterRangeLoc=3146&amp;EndingColumnNumber=12&amp;EndingLineNumber=73&amp;StartingColumnNumber=1&amp;StartingLineNumber=73&amp;Timestamp=424365435.370573">
9+
documentLocation = "#CharacterRangeLen=11&amp;CharacterRangeLoc=3383&amp;EndingColumnNumber=12&amp;EndingLineNumber=77&amp;StartingColumnNumber=1&amp;StartingLineNumber=77&amp;Timestamp=426611538.879704">
1010
</LoggerValueHistoryTimelineItem>
1111
</TimelineItems>
1212
</Timeline>

5. Control Flow.playground/section-1.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// ------------------------------------------------------------------------------------------------
1010
// For loops
1111
//
12-
// We can loop through ranges using the closed-range operator (three dots).
12+
// We can loop through ranges using the closed-range operator ("...").
1313
//
1414
// In the loop below, 'index' is a constant that is automatically declared.
1515
for index in 1...5
@@ -26,10 +26,10 @@ for index in 1...5
2626
//
2727
// index = 0
2828

29-
// We can loop through ranges using the half-closed range operator (with two dots)
29+
// We can loop through ranges using the half-closed range operator ("..<")
3030
//
3131
// We can also reuse the name 'index' because of the scoping noted previously.
32-
for index in 1..5
32+
for index in 1 ..< 5
3333
{
3434
"This will print 4 times"
3535
}

5. Control Flow.playground/timeline.xctimeline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=11004&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424365450.220564">
6+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10993&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=426611652.098414">
77
</LoggerValueHistoryTimelineItem>
88
</TimelineItems>
99
</Timeline>

0 commit comments

Comments
 (0)