swift study #3

23
Swift Study #3 #이상한모임 @신촌

Upload: chanju-jeon

Post on 10-May-2015

368 views

Category:

Software


0 download

DESCRIPTION

Swift Study #3 정리자료 / #이상한모임 @신촌

TRANSCRIPT

Page 1: Swift Study #3

Swift Study #3#이상한모임 @신촌

Page 2: Swift Study #3

Closures

Page 3: Swift Study #3

Closures

• Closures are self-contained blocks of functionality that can be passed around and used in your code.

• Closures can capture and store references to any constants and variables from the context in which they are defined.

Page 4: Swift Study #3

Closures take one of three forms:

• Global functions are closures that have a name and do not capture any values.

• Nested functions are closures that have a name and can capture values from their enclosing function.

• Closure expressions are unnamed closures written in a lightweight syntax that can be capture values from their surrounding context.

Page 5: Swift Study #3

The Sorted Function

let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] !func backwards(s1: String, s2: String) -> Bool { return s1 > s2 } !var reversed = sorted(names, backwards)

Page 6: Swift Study #3

Closure Expression Syntax

reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 })

Page 7: Swift Study #3

Inferring Type From Context

reversed = sorted(names, { s1, s2 in return s1 > s2 })

Page 8: Swift Study #3

Inferring Type From Context

reversed = sorted(names, { s1, s2 in return s1 > s2 })

Page 9: Swift Study #3

Implicit Returns from Single-Expression Closures

reversed = sorted(names, { s1, s2 in s1 > s2 })

Page 10: Swift Study #3

Shorthand Argument Names

reversed = sorted(names, { $0 > $1 })

Page 11: Swift Study #3

Operator Functions

reversed = sorted(names, >)

Page 12: Swift Study #3

Trailing Closures

reversed = sorted(names) { $0 > $1 }

Page 13: Swift Study #3

Trailing Closureslet digitNames = [ 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine" ] !let numbers = [16, 58, 510] !let strings = numbers.map { (var number) -> String in var output = "" while number > 0 { output = digitNames[number % 10]! + output number /= 10 } return output }

Page 14: Swift Study #3

Capturing Values

func makeIncrementor(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementor() -> Int { runningTotal += amount return runningTotal } return incrementor }

Page 15: Swift Study #3

Capturing Values

func makeIncrementor(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementor() -> Int { runningTotal += amount return runningTotal } return incrementor }

Page 16: Swift Study #3

Closures are Reference Types

let incrementByTen = makeIncrementor(forIncrement: 10) !incrementByTen() // return 10 incrementByTen() // return 20 incrementByTen() // return 30 !let alsoIncrementByTen = incrementByTen !alsoIncrementByTen() // return 40

Page 17: Swift Study #3

Enumerations

Page 18: Swift Study #3

Enumerations

• An enumeration define a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Page 19: Swift Study #3

Enumerations• Enumerations in Swift are much more flexible,

and do not have to provide a value for each member of the enumeration.

• Enumerations in Swift are first-class types in their own right.

• Enumerations can also define initializers … ; can be extended … ; and can conform to protocols …

Page 20: Swift Study #3

Enumeration Syntax

enum CompassPoint { case North case South case East case West } !enum Planet { case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune }

Page 21: Swift Study #3

Matching Enumeration Values with a Switch Statement

directionToHead = .South switch directionToHead { case .North: println("Lots of planets have a north") case .South: println("Watch out for penguins") case .East: println("Where the sun rises") case .West: println("Where the skies are blue") } // prints "Watch out for penguins"

Page 22: Swift Study #3

Associated Valuesenum Barcode { case UPCA(Int, Int, Int, Int) case QRCode(String) } !var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) productBarcode = .QRCode("ABCDEFGHIJKLMNOP") !switch productBarcode { case .UPCA(let numberSystem, let manufacturer, let product, let check): println("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).") case .QRCode(let productCode): println("QR code: \(productCode).") } // prints "QR code: ABCDEFGHIJKLMNOP."

Page 23: Swift Study #3

Raw Valuesenum ASCIIControlCharacter: Character { case Tab = "\t" case LineFeed = "\n" case CarriageReturn = "\r" } !enum Planet: Int { case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune } !let earthOrder = Planet.Earth.toRaw() // earthOrder is 3 !let possiblePlanet = Planet.fromRaw(7) // possiblePlanet is of type Planet? and equals Planet.Uranus