Your SlideShare is downloading. ×
Swift Study #2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Swift Study #2

667
views

Published on

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

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

Published in: Software, Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
667
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Swift Study #2 #이상한모임 @신촌
  • 2. Collection Types
  • 3. Collection Types • Arrays, Dictionary • Typed Collections • Generic Collections
  • 4. Mutability of Collections • var = mutable, let = immutable • let shoppingList = [“Eggs”, “Milk”]
 shoppingList[0] = [“Six Eggs”]
 
 b2: OK, b3: Error
  • 5. Arrays • Array Type Shorthand Syntax • Array<SomeType> -> SomeType[] //b2 • Array<SomeType> -> [SomeType] //b3 • var shoppingList: [String] = [“Eggs”, “Milk”]
  • 6. Accessing and Modifying an Array • shoppingList.count • shoppingList.isEmpty • shoppingList.append(“Flour”) • shoppingList += “Baking Powder” • shoppingList += [“Chocolate Spread”, “Cheese”, “Butter”]
  • 7. Accessing and Modifying an Array • shoppingList[0] = “Six Eggs” • shoppingList[4…6] = [“Bananas”, “Apples”] • shoppingList.insert(“Maple Syrup”, atIndex: 0) • let mapleSyrup = shoppingList.removeAtIndex(0) • let apples = shoppingList.removeLast()
  • 8. Iterating Over an Array • for item in shoppingList {
 println(item)
 } • for (index, value) in enumerate(shoppingList) {
 println(“(index+1): (value)”)
 }
  • 9. Creating and Initializing an Array • var someInts = [Int]()
 someInts.append(3)
 someInts = [] • var threeDoubles = [Double](count: 3, repeatedValue: 0.0) • var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) • var sixDoubles = threeDoubles + anotherThreeDoubles
  • 10. Dictionaries • Dictionary<KeyType, ValueType> -> [KeyType: ValueType] //b3 • var airports: [String: String] = [“TYO”: “Tokyo”, “DUB”: “Dublin”]
  • 11. Accessing and Modifying a Dictionary • airports.count • airports[“LHR”] = “London” • airports[“LHR”] = “London Heathrow”
  • 12. Accessing and Modifying a Dictionary • if let oldValue = airports.updateValue(“Dublin International”, forKey: “DUB”) {
 println(oldValue)
 } • if let airportName = airports[“DUB”] {
 println(airportName)
 } else {
 println(“not in the airports”)
 }
  • 13. Accessing and Modifying a Dictionary • airports[“APL”] = “Apple International”
 airports[“APL”] = nil • if let removedValue = airports.removeValueForKey(“DUB”) {
 println(“removed (removedValue)”)
 } else {
 println(“not contain a value”)
 }
  • 14. Iterating Over a Dictionary • for (airportCode, airportName) in airports {
 println(“(airportCode): (airportName)”)
 } • for airportCode in airports.keys • for airportName in airports.values
  • 15. Creating an Empty Dictionary • var namesOfIntegers = [Int: String]()
 namesOfIntegers[16] = “sixteen”
 namesOfIntegers = [:]
  • 16. Hash Values for Dictionary Key Types • A type must be hashable in order to be used as a dictionary’s key type — that is, the type must provide a way to compute a hash value for itself.
  • 17. Control Flow
  • 18. For-In • for index in 1…5 • for _ in 1…10 • for item in names • for (key, value) in json • for character in “Hello”
  • 19. (Do-)While • while condition {
 statements
 } • do {
 statements
 } while condition
  • 20. Conditional Statements • var temperatureInFahrenheit = 30
 if temperatureInFahrenheit <= 32 {
 println(“It’s very cold. Consider wearing a scarf.”)
 }
  • 21. Conditional Statements • var temperatureInFahrenheit = 40
 if temperatureInFahrenheit <= 32 {
 println(“It’s very cold. Consider wearing a scarf.”)
 } else {
 println(“It’s not that cold. Wear a t-shirt.”)
 }
  • 22. Conditional Statements • var temperatureInFahrenheit = 90
 if temperatureInFahrenheit <= 32 {
 println(“It’s very cold. Consider wearing a scarf.”)
 } else if temperatureInFahrenheit >= 86 {
 println(“It’s really warm. Don’t forget to wear sunscreen.”)
 } else {
 println(“It’s not that cold. Wear a t-shirt.”)
 }
  • 23. Conditional Statements • var temperatureInFahrenheit = 72
 if temperatureInFahrenheit <= 32 {
 println(“It’s very cold. Consider wearing a scarf.”)
 } else if temperatureInFahrenheit >= 86 {
 println(“It’s really warm. Don’t forget to wear sunscreen.”)
 }
  • 24. Switch • switch some value to consider {
 case value 1:
 respond to value 1
 case value 2,
 value 3:
 respond to value 2 or 3
 default:
 otherwise, do something else
 }
  • 25. No Implicit Fallthrough • switch anotherCharacter {
 case “a”:
 case “A”:
 println(“The letter A”)
 default:
 println(“Not the letter A”)
 }
 // this will report a compile-time error
  • 26. No Implicit Fallthrough • switch anotherCharacter {
 case “a”, “A”:
 println(“The letter A”)
 default:
 println(“Not the letter A”)
 } • use the fallthrough keyword
  • 27. Range Matching • switch count {
 case 0:
 naturalCount = “no”
 case 1…3:
 naturalCount = “a few”
 case 4…9:
 naturalCount = “several”
 case 10…99:
 naturalCount = “tens of”
 case 100…999:
 naturalCount = “hundreds of”
 case 1000…999_999:
 naturalCount = “thousands of”
 default:
 naturalCount = “millions and millions of”
 }
  • 28. Tuples • let somePoint = (1,1)
 switch somePoint {
 case (0,0):
 println(“at the origin”)
 case (_,0):
 println(“on the x-axis”)
 case (0,_):
 println(“on the y-axis”)
 case (-2…2,-2…2):
 println(“inside the box”)
 default:
 println(“outside of the box”)
 }
  • 29. Value Bindings • var anotherPoint = (2,0)
 switch anotherPoint {
 case (let x, 0):
 println(“on the x-axis an x value of (x)”)
 case (0, let y):
 println(“on the y-axis an y value of (y)”)
 case let (x,y):
 println(“somewhere else at ((x), (y))”)
 }
  • 30. Where • let yetAnotherPoint = (1,-1)
 switch yetAnotherPoint {
 case let (x,y) where x == y:
 println(“on the line x == y”)
 case let (x,y) where x == -y:
 println(“on the line x == -y”)
 case let (x,y):
 println(“just some arbitrary point”)
 }
  • 31. Control Transfer Statements • continue • break • fallthrough • return
  • 32. Fallthrough • let integerToDescribe = 5
 switch integerToDescribe {
 case 2,3,5,7,11,13,17,19:
 description += " a prime number, and also”
 fallthrough
 default:
 description += “ an integer.”
 }
  • 33. Labeled Statements • Goto • break, continue
  • 34. Functions
  • 35. Defining and Calling Functions • func sayHello(personName: String) -> String {
 let greeting = “Hello, ” + personName + “!”
 return greeting
 }
 
 println(sayHello(“Anna”))
 // prints “Hello, Anna!”
 println(sayHello(“Brian”))
 // prints “Hello, Brian!”
  • 36. Function Parameters and Return Values • Multiple Input Parameters • func halfOpenRangeLength(start: Int, end: Int) -> Int • Functions Without Parameters • func sayHelloWorld() -> String • Functions Without Return Values • func sayGoodbye(personName: String) • Functions with Multiple Return Values • return (vowels, consonants, others)
  • 37. Function Parameter Names • External Parameter Names • func join(s1: String, s2: String, joiner: String) -> String {
 return s1 + joiner + s2
 } • join(“hello”, “world”, “, ”)
 // returns “hello, world”
  • 38. Function Parameter Names • External Parameter Names • func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String {
 return s1 + joiner + s2
 } • join(string: “hello”, toString: “world”, withJoiner: “, ”)
 // returns “hello, world”
  • 39. Shorthand External Parameter Names • func containsCharacter(#string: String, #characterToFind: Character) -> Bool { // blah blah } • let containsAVee = containsCharacter(string: “aardvark”, characterToFind: “v”)
  • 40. Default Parameter Values • func join(string s1: String, toString s2: String, withJoiner joiner: String = “ ”) - > String {
 return s1 + joiner + s2
 } • join(string: “hello”, toString: “world”, withJoiner: “-”)
 // returns “hello-world” • join(string: “hello”, toString: “world”)
 // returns “hello world”
  • 41. External Names for Parameters with Default Values • func join(s1: String, s2: String, joiner: String = “ ”) -> String {
 return s1 + joiner + s2
 } • join(“hello”,“world”, joiner: “-”)
 // returns “hello-world”
  • 42. Variadic Parameters • func arithmeticMean(numbers: Double…) - > Double {
 var total: Double = 0
 for number in numbers {
 total += number
 }
 return total / Double(number.count)
 }
 
 arithmeticMean(1, 2, 3, 4, 5)
 arithmeticMean(3, 8, 19)
  • 43. Constant and Variable Parameters • func alignRight(var string: String, count: Int, pad: Character) -> String {
 let amountToPad = count - countElements(string)
 if amountToPad < 1 {
 return string
 }
 for _ in 1…amountToPad {
 string += pad + string
 }
 return string
 }
 
 let originalString = “hello”
 let paddedString = alignRight(originalString, 10, “-”)
  • 44. In-Out Parameters • func swapTwoInts(inout a: Int, inout b: Int) {
 let temporaryA = a
 a = b
 b = temporaryA
 }
 
 var someInt = 3
 var anotherInt = 107
 swapTwoInts(&someInt, &anotherInt)
  • 45. Function Types • func addTwoInts(a: Int, b: Int) -> Int { return a + b }
 func multiplyTwoInts(a: Int, b: Int) -> Int { return a * b }
 // (Int, Int) -> Int • func printHelloWorld() {
 println(“hello, world”)
 }
 // () -> ()
  • 46. Using Function Types • var mathFunction: (Int, Int) -> Int = addTwoInts
 println(mathFunction(2,3)) • mathFunction = multiplyTwoInts
 println(mathFunction(2,3)) • let anotherMathFunction = addTwoInts
  • 47. Function Types as Parameter Types • func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
 println(“Result: (mathFunction(a,b))”)
 }
 
 printMathResult(addTwoInts,3,5)
 // prints “Result: 8”
  • 48. Function Types as Return Types • func stepForward(input: Int) -> Int {
 return input + 1
 }
 func stepBackward(input: Int) -> Int {
 return input - 1
 }
 
 func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
 return backwards ? stepBackward : stepForward
 }
 
 let moveNearerToZero = chooseStepFunction(currentValue > 0)
 // blah blah
 currentValue = moveNearerToZero(currentValue)

  • 49. Nested Functions • func chooseStepFunction(backward: Bool) -> (Int) -> Int {
 func stepForward(input: Int) -> Int { return input + 1 }
 func stepBackward(input: Int) -> Int { return input - 1 }
 return backwards ? stepBackward: stepForward
 }