From dd5a3cb1ba029981164ecb25d6a838ae73a40fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Wo=CC=81jcik?= Date: Tue, 8 Jul 2014 16:07:17 +0200 Subject: [PATCH] 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 --- 10. Properties.playground/section-1.swift | 4 +- 12. Subscripts.playground/section-1.swift | 2 +- .../section-1.swift | 4 +- 18. Type Casting.playground/section-1.swift | 8 ++-- .../section-1.swift | 2 +- 20. Extensions.playground/section-1.swift | 2 +- 21. Protocols.playground/section-1.swift | 4 +- 22. Generics.playground/section-1.swift | 8 ++-- 4a. Arrays.playground/section-1.swift | 23 ++++++---- 4b. Dictionaries.playground/section-1.swift | 4 ++ 5. Control Flow.playground/section-1.swift | 2 +- 7. Closures.playground/section-1.swift | 43 +++++++++++-------- 12 files changed, 62 insertions(+), 44 deletions(-) diff --git a/10. Properties.playground/section-1.swift b/10. Properties.playground/section-1.swift index 9a7c9f0..15cfa7f 100644 --- a/10. Properties.playground/section-1.swift +++ b/10. Properties.playground/section-1.swift @@ -44,7 +44,7 @@ class DataImporter class DataManager { @lazy var importer = DataImporter() - var data = String[]() + var data = [String]() } // Now let's instantiate the data manager and add some simple data to the class: @@ -297,4 +297,4 @@ class SomeClass // This is read-only, but you can also do read/write class var computedTypeProperty: Int { return 4 } -} \ No newline at end of file +} diff --git a/12. Subscripts.playground/section-1.swift b/12. Subscripts.playground/section-1.swift index 12598f1..55ded36 100644 --- a/12. Subscripts.playground/section-1.swift +++ b/12. Subscripts.playground/section-1.swift @@ -44,7 +44,7 @@ struct Matrix { let rows: Int let columns: Int - var grid: Double[] + var grid: [Double] init (rows: Int, columns: Int) { diff --git a/14b. Initializer Chaining.playground/section-1.swift b/14b. Initializer Chaining.playground/section-1.swift index 9fefcf0..dfb6626 100644 --- a/14b. Initializer Chaining.playground/section-1.swift +++ b/14b. Initializer Chaining.playground/section-1.swift @@ -163,9 +163,9 @@ class ClassWithPI // initialized and then returned. struct CheckerBoard { - let boardColors: Bool[] = + let boardColors: [Bool] = { - var temporaryBoard = Bool[]() + var temporaryBoard = [Bool]() var isBlack = false for i in 1...10 { diff --git a/18. Type Casting.playground/section-1.swift b/18. Type Casting.playground/section-1.swift index 9a8b179..0d8ce0d 100644 --- a/18. Type Casting.playground/section-1.swift +++ b/18. Type Casting.playground/section-1.swift @@ -107,7 +107,7 @@ for item in library // // Let's see AnyObject in action. We'll define an array of type AnyObject[] and populate it with // some movies: -let someObjects: AnyObject[] = +let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director: "Duncan Jones"), @@ -127,7 +127,7 @@ for object: AnyObject in someObjects } // Alternatively, we can downcast the array itself rather than each item: -var someMovies = someObjects as Movie[] +var someMovies = someObjects as [Movie] for movie in someMovies { "Movie: '\(movie.name)' was directed by \(movie.director)" @@ -135,7 +135,7 @@ for movie in someMovies // Finally, we can avoid the additional local variable and performt he downcast right inside // the loop structure: -for movie in someObjects as Movie[] +for movie in someObjects as [Movie] { "Movie: '\(movie.name)' was directed by \(movie.director)" } @@ -145,7 +145,7 @@ for movie in someObjects as Movie[] // // Let's see this in action. We'll create an array of type Any[] and fill it with random bits and // pieces of stuff: -var things = Any[]() +var things = [Any]() things.append(0) things.append(0.0) diff --git a/2. Basic operations.playground/section-1.swift b/2. Basic operations.playground/section-1.swift index dcdbe0b..5dab6f2 100644 --- a/2. Basic operations.playground/section-1.swift +++ b/2. Basic operations.playground/section-1.swift @@ -36,7 +36,7 @@ var d = a % b // Floating point remainder // The range operator with two dots means up to but NOT including the final value. // // This is called the "Half-Closed Range Operator" -for i in 1..10 +for i in 1..<10 { i // prints 1 through 9 } diff --git a/20. Extensions.playground/section-1.swift b/20. Extensions.playground/section-1.swift index 02d3494..797b33e 100644 --- a/20. Extensions.playground/section-1.swift +++ b/20. Extensions.playground/section-1.swift @@ -108,7 +108,7 @@ extension Int { func repititions(task: () -> ()) { - for i in 0..self + for i in 0.. { - var items = T[]() + var items = [T]() mutating func push(item: T) { items.append(item) @@ -133,7 +133,7 @@ func doSomethingWithKeyValue(someKey: KeyType, som // element from the array with the value being searched for. By including the Equatable, we tell // the generic function that it is guaranteed to receive only values that meet that specific // criteria. -func findIndex(array: T[], valueToFind: T) -> Int? +func findIndex(array: [T], valueToFind: T) -> Int? { for (index, value) in enumerate(array) { @@ -177,7 +177,7 @@ struct StackContainer : Container { // Here we find our original stack implementation, unmodified - var items = T[]() + var items = [T]() mutating func push(item: T) { items.append(item) @@ -251,7 +251,7 @@ func allItemsMatch } // Check each pair of items to see if they are equivalent - for i in 0..someContainer.count + for i in 0..() // Shorter, more common way to define an array of Strings -var shorter: String[] +var shorter: [String] // This is an array literal. Since all members are of type String, this will create a String array. // @@ -28,7 +28,7 @@ var shorter: String[] ["Eggs", "Milk"] // Let's create an array with some stuff in it. We'll use an explicit String type: -var commonPets: String[] = ["Cats", "Dogs"] +var commonPets: [String] = ["Cats", "Dogs"] // We can also let Swift infer the type of the Array based on the type of the initializer members. // @@ -71,7 +71,7 @@ shoppingList[0] = "Six Eggs" shoppingList[4...6] = ["Banannas", "Apples"] // Or we can replace two items with three, inserting a new item: -shoppingList[4..6] = ["Limes", "Mint leaves", "Sugar"] +shoppingList[4..<6] = ["Limes", "Mint leaves", "Sugar"] // We can insert an item at a given index shoppingList.insert("Maple Syrup", atIndex: 3) @@ -102,7 +102,7 @@ for (index, value) in enumerate(shoppingList) // // Earlier, we saw how to declare an array of a given type. Here, we see how to declare an array // type and then assign it to a stored value, which gets its type by inference: -var someInts = Int[]() +var someInts = [Int]() // Add the number '3' to the array someInts.append(3) @@ -113,7 +113,7 @@ someInts someInts = [] // We can initialize an array and and fill it with default values -var threeDoubles = Double[](count: 3, repeatedValue: 3.3) +var threeDoubles = [Double](count: 3, repeatedValue: 3.3) // We can also use the Array initializer to fill it with default values. Note that we don't need to // specify type since it is inferred: @@ -127,7 +127,8 @@ let immutableArray = ["a", "b"] // the array itself. // // We change the contents of an immutable array: -immutableArray[0] = "b" +// immutableArray[0] = "b" +// !!! Since beta 3 this operation also give compiler error !!! // But if you try to change the size or add an element, you will get a compiler error: // @@ -169,7 +170,9 @@ c // // The unshare() method is performant because it doesn't actually copy the array contents until // it has to (if ever.) -b.unshare() +// b.unshare() +// !!! Since beta 3 Apple remove this method, copy using an efficient lazy copy implementation !!! + // They still appear to be the same... b @@ -207,6 +210,10 @@ else // Use the copy() method to force a shallow copy. // // Unlike the unshare method, the copy will happen immediately when calling copy(). -var d = a.copy() +// var d = a.copy() +// !!! Since beta 3 Apple remove this method, copy using an efficient lazy copy implementation !!! +// So you can use simple assign operator +var d = a + a[0] = 101 d[0] diff --git a/4b. Dictionaries.playground/section-1.swift b/4b. Dictionaries.playground/section-1.swift index b7b1bf3..a5dd335 100644 --- a/4b. Dictionaries.playground/section-1.swift +++ b/4b. Dictionaries.playground/section-1.swift @@ -119,3 +119,7 @@ copiedAges["Peter"] = 24 // And we can see that the original is not changed: ages["Peter"] + +// Since beta 3 you can also use [ KeyType: ValueType ] syntax to declare dictionary +var beta3dict:[String: AnyObject] + diff --git a/5. Control Flow.playground/section-1.swift b/5. Control Flow.playground/section-1.swift index 369d3b2..8ff4f61 100644 --- a/5. Control Flow.playground/section-1.swift +++ b/5. Control Flow.playground/section-1.swift @@ -29,7 +29,7 @@ for index in 1...5 // We can loop through ranges using the half-closed range operator (with two dots) // // We can also reuse the name 'index' because of the scoping noted previously. -for index in 1..5 +for index in 1..<5 { "This will print 4 times" } diff --git a/7. Closures.playground/section-1.swift b/7. Closures.playground/section-1.swift index 02cd8fe..28a9d8f 100644 --- a/7. Closures.playground/section-1.swift +++ b/7. Closures.playground/section-1.swift @@ -33,11 +33,11 @@ // special attention to the curly braces that encapsulate the closure and the parenthesis just // outside of those curly braces: let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] -var reversed = String[]() -reversed = sort(names, - { (s1: String, s2: String) -> Bool in - return s1 > s2 - }) +var reversed = [String]() +reversed = names.sorted({ + (s1: String, s2: String) -> Bool in + return s1 > s2 +}) // ------------------------------------------------------------------------------------------------ // Inferring Type from Context @@ -49,28 +49,28 @@ reversed = sort(names, // call to sort. // // The following call is identical to the one above with the exception that "-> Bool" was removed: -reversed = sort(names, - { (s1: String, s2: String) in +reversed = names.sorted({ + (s1: String, s2: String) in return s1 > s2 - }) +}) // Just as the return type can be inferred, so can the parameter types. This allows us to simplify // the syntax a bit further by removing the type annotations from the closure's parameters. // // The following call is identical to the one above with the exception that the parameter type // annotations (": String") have been removed: -reversed = sort(names, - { (s1, s2) in +reversed = names.sorted({ + (s1, s2) in return s1 > s2 - }) +}) // Since all types can be inferred, we can simplify a bit further by removing the paranthesis // around the parameters. We'll also put it all on a single line, since it's a bit more clear now: -reversed = sort(names, { s1, s2 in return s1 > s2 }) +reversed = names.sorted({ s1, s2 in return s1 > s2 }) // If the closuere has only a single expression, then the return statement is also inferred. When // this is the case, the closure returns the value of the single expression. -reversed = sort(names, { s1, s2 in s1 > s2 }) +reversed = names.sorted({ s1, s2 in s1 > s2 }) // We're not done simplifying yet. It turns out we can get rid of the parameters as well. If we // 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 }) // // Here's what that would might like (this will not compile - yet): // -// reversed = sort(names, { s1, s2 in $0 > $1 }) +// reversed = names.sorted({ s1, s2 in $0 > $1 }) // // This won't compile because you're not allowed to use shorthand names if you specify the // parameter names. Therefore, we need to remove those in order to get it to compile. This makes // for a very short inline closure: -reversed = sort(names, { $0 > $1 }) +reversed = names.sorted({ $0 > $1 }) // Interestingly enough, the operator < for String types is defined as: // @@ -95,7 +95,14 @@ reversed = sort(names, { $0 > $1 }) // exactly this. // // Here's what that looks like: -reversed = sort(names, >) +reversed = names.sorted(>) + +// If you want just sort mutable copy of array you can use sort method +var mutableCopyOfNames = names + +mutableCopyOfNames.sort(>) + +mutableCopyOfNames // ------------------------------------------------------------------------------------------------ // Trailing Closures @@ -107,7 +114,7 @@ reversed = sort(names, >) // // Let's go back to our original call to sort with a fully-formed closure and move the closure // outside of the parameter list. This resembles a function definition, but it's a function call. -reversed = sort(names) { +reversed = names.sorted { (s1: String, s2: String) -> Bool in return s1 > s2 } @@ -123,7 +130,7 @@ reversed = sort(names) { // } // Let's jump back to our simplified closure ({$0 > $1}) and apply the trailing closure principle: -reversed = sort(names) {$0 > $1} +reversed = names.sorted {$0 > $1} // Another simplification: if a function receives just one closure as the only parameter, you can // remove the () from the function call. First, we'll need a function that receives just one