Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 3: Enumerations

25. Challenge: Switch Statements

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

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

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.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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❌