swift study #3
DESCRIPTION
Swift Study #3 정리자료 / #이상한모임 @신촌TRANSCRIPT
![Page 1: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/1.jpg)
Swift Study #3#이상한모임 @신촌
![Page 2: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/2.jpg)
Closures
![Page 3: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/6.jpg)
Closure Expression Syntax
reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 })
![Page 7: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/7.jpg)
Inferring Type From Context
reversed = sorted(names, { s1, s2 in return s1 > s2 })
![Page 8: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/8.jpg)
Inferring Type From Context
reversed = sorted(names, { s1, s2 in return s1 > s2 })
![Page 9: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/9.jpg)
Implicit Returns from Single-Expression Closures
reversed = sorted(names, { s1, s2 in s1 > s2 })
![Page 10: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/10.jpg)
Shorthand Argument Names
reversed = sorted(names, { $0 > $1 })
![Page 11: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/11.jpg)
Operator Functions
reversed = sorted(names, >)
![Page 12: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/12.jpg)
Trailing Closures
reversed = sorted(names) { $0 > $1 }
![Page 13: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/17.jpg)
Enumerations
![Page 18: Swift Study #3](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.pub/reader036/viewer/2022062405/554f44b7b4c905423f8b4825/html5/thumbnails/23.jpg)
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