Skip to content

Commit dd5a3cb

Browse files
author
Rafał Wójcik
committed
Updated to use Swift latest features introduced with beta 3.
https://9to5mac.files.wordpress.com/2014/07/screen-shot-2014-07-07-at-19-26-39.png
1 parent f2f0b2d commit dd5a3cb

File tree

12 files changed

+62
-44
lines changed

12 files changed

+62
-44
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: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
var someArray = Array<String>()
2020

2121
// Shorter, more common way to define an array of Strings
22-
var shorter: String[]
22+
var shorter: [String]
2323

2424
// This is an array literal. Since all members are of type String, this will create a String array.
2525
//
@@ -28,7 +28,7 @@ var shorter: String[]
2828
["Eggs", "Milk"]
2929

3030
// Let's create an array with some stuff in it. We'll use an explicit String type:
31-
var commonPets: String[] = ["Cats", "Dogs"]
31+
var commonPets: [String] = ["Cats", "Dogs"]
3232

3333
// We can also let Swift infer the type of the Array based on the type of the initializer members.
3434
//
@@ -71,7 +71,7 @@ shoppingList[0] = "Six Eggs"
7171
shoppingList[4...6] = ["Banannas", "Apples"]
7272

7373
// Or we can replace two items with three, inserting a new item:
74-
shoppingList[4..6] = ["Limes", "Mint leaves", "Sugar"]
74+
shoppingList[4..<6] = ["Limes", "Mint leaves", "Sugar"]
7575

7676
// We can insert an item at a given index
7777
shoppingList.insert("Maple Syrup", atIndex: 3)
@@ -102,7 +102,7 @@ for (index, value) in enumerate(shoppingList)
102102
//
103103
// Earlier, we saw how to declare an array of a given type. Here, we see how to declare an array
104104
// type and then assign it to a stored value, which gets its type by inference:
105-
var someInts = Int[]()
105+
var someInts = [Int]()
106106

107107
// Add the number '3' to the array
108108
someInts.append(3)
@@ -113,7 +113,7 @@ someInts
113113
someInts = []
114114

115115
// We can initialize an array and and fill it with default values
116-
var threeDoubles = Double[](count: 3, repeatedValue: 3.3)
116+
var threeDoubles = [Double](count: 3, repeatedValue: 3.3)
117117

118118
// We can also use the Array initializer to fill it with default values. Note that we don't need to
119119
// specify type since it is inferred:
@@ -127,7 +127,8 @@ let immutableArray = ["a", "b"]
127127
// the array itself.
128128
//
129129
// We change the contents of an immutable array:
130-
immutableArray[0] = "b"
130+
// immutableArray[0] = "b"
131+
// !!! Since beta 3 this operation also give compiler error !!!
131132

132133
// But if you try to change the size or add an element, you will get a compiler error:
133134
//
@@ -169,7 +170,9 @@ c
169170
//
170171
// The unshare() method is performant because it doesn't actually copy the array contents until
171172
// it has to (if ever.)
172-
b.unshare()
173+
// b.unshare()
174+
// !!! Since beta 3 Apple remove this method, copy using an efficient lazy copy implementation !!!
175+
173176

174177
// They still appear to be the same...
175178
b
@@ -207,6 +210,10 @@ else
207210
// Use the copy() method to force a shallow copy.
208211
//
209212
// Unlike the unshare method, the copy will happen immediately when calling copy().
210-
var d = a.copy()
213+
// var d = a.copy()
214+
// !!! Since beta 3 Apple remove this method, copy using an efficient lazy copy implementation !!!
215+
// So you can use simple assign operator
216+
var d = a
217+
211218
a[0] = 101
212219
d[0]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,7 @@ copiedAges["Peter"] = 24
119119

120120
// And we can see that the original is not changed:
121121
ages["Peter"]
122+
123+
// Since beta 3 you can also use [ KeyType: ValueType ] syntax to declare dictionary
124+
var beta3dict:[String: AnyObject]
125+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ for index in 1...5
2929
// We can loop through ranges using the half-closed range operator (with two dots)
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
}

7. Closures.playground/section-1.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
// special attention to the curly braces that encapsulate the closure and the parenthesis just
3434
// outside of those curly braces:
3535
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
36-
var reversed = String[]()
37-
reversed = sort(names,
38-
{ (s1: String, s2: String) -> Bool in
39-
return s1 > s2
40-
})
36+
var reversed = [String]()
37+
reversed = names.sorted({
38+
(s1: String, s2: String) -> Bool in
39+
return s1 > s2
40+
})
4141

4242
// ------------------------------------------------------------------------------------------------
4343
// Inferring Type from Context
@@ -49,28 +49,28 @@ reversed = sort(names,
4949
// call to sort.
5050
//
5151
// The following call is identical to the one above with the exception that "-> Bool" was removed:
52-
reversed = sort(names,
53-
{ (s1: String, s2: String) in
52+
reversed = names.sorted({
53+
(s1: String, s2: String) in
5454
return s1 > s2
55-
})
55+
})
5656

5757
// Just as the return type can be inferred, so can the parameter types. This allows us to simplify
5858
// the syntax a bit further by removing the type annotations from the closure's parameters.
5959
//
6060
// The following call is identical to the one above with the exception that the parameter type
6161
// annotations (": String") have been removed:
62-
reversed = sort(names,
63-
{ (s1, s2) in
62+
reversed = names.sorted({
63+
(s1, s2) in
6464
return s1 > s2
65-
})
65+
})
6666

6767
// Since all types can be inferred, we can simplify a bit further by removing the paranthesis
6868
// around the parameters. We'll also put it all on a single line, since it's a bit more clear now:
69-
reversed = sort(names, { s1, s2 in return s1 > s2 })
69+
reversed = names.sorted({ s1, s2 in return s1 > s2 })
7070

7171
// If the closuere has only a single expression, then the return statement is also inferred. When
7272
// this is the case, the closure returns the value of the single expression.
73-
reversed = sort(names, { s1, s2 in s1 > s2 })
73+
reversed = names.sorted({ s1, s2 in s1 > s2 })
7474

7575
// We're not done simplifying yet. It turns out we can get rid of the parameters as well. If we
7676
// remove the parameters, we can still access them because Swift provides shorthand names to
@@ -79,12 +79,12 @@ reversed = sort(names, { s1, s2 in s1 > s2 })
7979
//
8080
// Here's what that would might like (this will not compile - yet):
8181
//
82-
// reversed = sort(names, { s1, s2 in $0 > $1 })
82+
// reversed = names.sorted({ s1, s2 in $0 > $1 })
8383
//
8484
// This won't compile because you're not allowed to use shorthand names if you specify the
8585
// parameter names. Therefore, we need to remove those in order to get it to compile. This makes
8686
// for a very short inline closure:
87-
reversed = sort(names, { $0 > $1 })
87+
reversed = names.sorted({ $0 > $1 })
8888

8989
// Interestingly enough, the operator < for String types is defined as:
9090
//
@@ -95,7 +95,14 @@ reversed = sort(names, { $0 > $1 })
9595
// exactly this.
9696
//
9797
// Here's what that looks like:
98-
reversed = sort(names, >)
98+
reversed = names.sorted(>)
99+
100+
// If you want just sort mutable copy of array you can use sort method
101+
var mutableCopyOfNames = names
102+
103+
mutableCopyOfNames.sort(>)
104+
105+
mutableCopyOfNames
99106

100107
// ------------------------------------------------------------------------------------------------
101108
// Trailing Closures
@@ -107,7 +114,7 @@ reversed = sort(names, >)
107114
//
108115
// Let's go back to our original call to sort with a fully-formed closure and move the closure
109116
// outside of the parameter list. This resembles a function definition, but it's a function call.
110-
reversed = sort(names) {
117+
reversed = names.sorted {
111118
(s1: String, s2: String) -> Bool in
112119
return s1 > s2
113120
}
@@ -123,7 +130,7 @@ reversed = sort(names) {
123130
// }
124131

125132
// Let's jump back to our simplified closure ({$0 > $1}) and apply the trailing closure principle:
126-
reversed = sort(names) {$0 > $1}
133+
reversed = names.sorted {$0 > $1}
127134

128135
// Another simplification: if a function receives just one closure as the only parameter, you can
129136
// remove the () from the function call. First, we'll need a function that receives just one

0 commit comments

Comments
 (0)