Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 3: Enumerations

25. Challenge: Switch Statements

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 24. More Switch Statements Next episode: 26. Associated Values

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 25. Challenge: Switch Statements

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Your next challenge is here! This time you’ll be testing yourself on switch statements. You’ll find everything you need on page 6 of the Playground for this part of the course. Pause the video, give it your best try, and then come back to check my solution.

let lifeStage: String
switch ("Ozma", 7) {

}
case (let name, 0...2):
case (let name, 0...2):
  😺lifeStage = "\(name) is Infant."
case (let name, 3...12):
  lifeStage = "\(name) is Child."
case (let name, 13...19):
  lifeStage = "\(name) is Teenager."
case (let name, 20...39):
  lifeStageForName = "\(name) is Adult."
case (let name, 40...60):
  lifeStage = "\(name) is Middle aged."
case (let name, 61...):
  lifeStage = "\(name) is Eldery."
case (_, let age):
  lifeStage = "Unaccounted for age: \(age)."
enum Direction {
  case north, south, east, west
}
func getLocation(for movements: [Direction])
func getLocation(for movements: [Direction])😺 -> (x: Int, y: Int) {

}
  var location = (x: 0, y: 0)
for movement in movements {

}
    switch movement {
    case .north:
      location.y += 1
    case .south:
      location.y -= 1
    case .east:
      location.x += 1
    case .west:
      location.x -= 1
  return location
getLocation(for: [.north, .west, .west])
❌  var location = (x: 0, y: 0)❌
movements.reduce(into: 
movements.reduce(into: 😺(x: 0, y: 0))🛑 
movements.reduce(into: (x: 0, y: 0)) 😺{ (location, movement) in 🛑
...
  case .west:
    location.x -= 1
  }
😺}🛑
  ❌return location❌