Download - Swift Study #3
Swift Study #3#이상한모임 @신촌
Closures
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.
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.
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)
Closure Expression Syntax
reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 })
Inferring Type From Context
reversed = sorted(names, { s1, s2 in return s1 > s2 })
Inferring Type From Context
reversed = sorted(names, { s1, s2 in return s1 > s2 })
Implicit Returns from Single-Expression Closures
reversed = sorted(names, { s1, s2 in s1 > s2 })
Shorthand Argument Names
reversed = sorted(names, { $0 > $1 })
Operator Functions
reversed = sorted(names, >)
Trailing Closures
reversed = sorted(names) { $0 > $1 }
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 }
Capturing Values
func makeIncrementor(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementor() -> Int { runningTotal += amount return runningTotal } return incrementor }
Capturing Values
func makeIncrementor(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementor() -> Int { runningTotal += amount return runningTotal } return incrementor }
Closures are Reference Types
let incrementByTen = makeIncrementor(forIncrement: 10) !incrementByTen() // return 10 incrementByTen() // return 20 incrementByTen() // return 30 !let alsoIncrementByTen = incrementByTen !alsoIncrementByTen() // return 40
Enumerations
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.
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 …
Enumeration Syntax
enum CompassPoint { case North case South case East case West } !enum Planet { case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune }
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"
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."
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