Skip to content

Commit

Permalink
Upgraded book to Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Antonio Karmy committed Jul 9, 2016
1 parent 2be0e47 commit c5b4907
Show file tree
Hide file tree
Showing 119 changed files with 1,308 additions and 2,105 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion 02_Basic Operators.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ for index in 0..<array.count{
}

// Enumerate array with index and value, C loop will be removed soon
for (index, value) in array.enumerate() {
for (index, value) in array.enumerated() {
print("value \(value) at index \(index)")
}

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ shoppingList += ["Bird", "Shark"]

shoppingList[1...3] = ["Bananas", "Apples", "Strawberries"] //Replace several items at once

shoppingList.insert("Maple Syrup", atIndex: 0) //Inserts element at index
shoppingList.insert("Maple Syrup", at: 0) //Inserts element at index

let mapleSyrup = shoppingList.removeAtIndex(0) // Returns removed item
let mapleSyrup = shoppingList.remove(at: 0) // Returns removed item

var emptyArray = [Int]() //Initialize empty array
var anotherEmptyArray = [] //Also valid
var array = [Int](count: 3, repeatedValue: 0) //Initalizes an array of length 3 with zeros
var array = [Int](repeating: 0, count: 3) //Initalizes an array of length 3 with zeros

var compoundArray = array + emptyArray

var reversedShoppingList: [String] = shoppingList.reverse()
var reversedShoppingList: [String] = shoppingList.reversed()

reversedShoppingList.removeLast() // Removes last item. Remove the first with removeFirst(). No returned value.
reversedShoppingList.popLast() // Pops the last item, removing it from the array and also returning it. Note that if the array is empty, the returned value is nil.
Expand All @@ -68,7 +68,7 @@ if let airportName = airports["DUB"] { //Subscript always returns optional in ca
}

airports["LAX"] = nil
airports.removeValueForKey("LAX") //Both remove the key-value pair
airports.removeValue(forKey: "LAX") //Both remove the key-value pair

//Iterating over the whole dictionary
for (airportCode, airportName) in airports {
Expand Down

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@ master: while true {
continue master
}
}

This file was deleted.

Binary file not shown.
Binary file not shown.
162 changes: 0 additions & 162 deletions 05_Functions.playground/section-1.swift.orig

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func arithmeticMean(numbers: Double...) -> Double {

//In this example, the inout prefix defines that the passed parameters' values can be modified,
//and this will be reflected on the original variables defined outside of the function.
func swapTwoInts(inout a: Int, inout b: Int) {
func swapTwoInts( a: inout Int, b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
Expand All @@ -62,18 +62,18 @@ func chooseStepFunction(backwards: Bool) -> (Int) -> Int {


//Start
sayHello("Juan")
sayHello(personName: "Juan")

minMax([])
minMax(array: [])

join(string: "Hello", toString: "World", withJoiner: "New")
join(string: "", toString: "")

arithmeticMean(4,5,6,7)
arithmeticMean(numbers: 4,5,6,7)

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, b: &anotherInt)
swapTwoInts(a: &someInt, b: &anotherInt)

//Here we are defining a var of type function.
var mathFunction: (String) -> String = sayHello
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
===============
*/

let array = ["John", "Tim", "Steve"]
var array = ["John", "Tim", "Steve"]

var reversed = array.sort({(s1: String, s2: String) -> Bool in return s1 > s2})
var reversed = array.sorted(isOrderedBefore: {(s1: String, s2: String) -> Bool in return s1 > s2})

//Using type inference, we can omit the params and return types. This is true when passing closures as params to a function.
reversed = array.sort({s1, s2 in return s1 > s2})
reversed = array.sorted(isOrderedBefore: {s1, s2 in return s1 > s2})

//In case of single-expression closures, the return value is implicit, thus the return expression can be omitted.
reversed = array.sort({s1, s2 in s1 == s2})
reversed = array.sorted(isOrderedBefore: {s1, s2 in s1 == s2})

//In the previous examples, the names of the closure's params were explicit. You can use the $X variables to refer to params for the closure.
//This eliminates the need for the first params list, which makes the body the only relevant part.
reversed = array.sort({$0 == $1})
reversed = array.sorted(isOrderedBefore: {$0 == $1})

//We can even take this to an extreme. String defines its own implementation for the ">" operator, which is really all the closure does.
reversed = array.sort(>)
reversed = array.sorted(isOrderedBefore: >)

/*##### TRAILING CLOSURES #####*/
func someFunctionThatTakesAClosure(closure: () -> ()) {
Expand All @@ -39,7 +39,7 @@ func someFunctionThatTakesAClosure(closure: () -> ()) {

//Closures which are too long to be defined inline.
// here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure({
someFunctionThatTakesAClosure(closure: {
// closure's body goes here
})

Expand Down Expand Up @@ -70,3 +70,32 @@ let stringsArray = numbers.map {
}
return output
}

/*
============
AUTOCLOSURES
============
*/

/*
An autoclosure is a closure that is automatically created to wrap an expression that’s being passed as an argument to a function.
It doesn’t take any arguments, and when it’s called, it returns the value of the expression that’s wrapped inside of it.
This syntactic convenience lets you omit braces around a function’s parameter by writing a normal expression instead of an explicit closure.

An autoclosure lets you delay evaluation, because the code inside isn’t run until you call the closure.
*/

var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
let customerProvider = { customersInLine.remove(at: 0) }

// Traditional way
func serve(customer customerProvider: () -> String) {
print("Now serving \(customerProvider())!") //The closure is called here!
}
serve(customer: { customersInLine.remove(at: 0) } )

// @autoclosure way
func serve(customer customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0)) //We are not required to use the curly braces, since the code will be wrapped in a closure thanks to @autoclosure

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
File renamed without changes.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
Loading

0 comments on commit c5b4907

Please sign in to comment.