Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 3: Enumerations

26. Associated Values

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: 25. Challenge: Switch Statements Next episode: 27. Conclusion

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.

Now that you’ve got a handle on switch statements, there’s one more important thing to cover about enumerations.

enum TwoDimensionalPoint {

}
case origin
case onXAxis(Double)
case onYAxis(Double)
case noZeroCoordinate(x: Double, y: Double)
let coordinates = (1.0, 3.0)
let twoDimensionalPoint: TwoDimensionalPoint
switch coordinates {
default: 
  twoDimensionalPoint = .noZeroCoordinate(x: coordinates.0, y: coordinates.1)
}
case (0, 0): 
  twoDimensionalPoint = .origin
let coordinates = (0.0, 0.0)
case (_, 0):
  twoDimensionalPoint = .onXAxis(coordinates.0)
case (0, _):
  twoDimensionalPoint = .onYAxis(coordinates.1)
let coordinates = (0.0, 7.0)
func getValues(for point: TwoDimensionalPoint) -> (x: Double, y: Double) {

}
switch point {

}
case .origin:
  return (0, 0)
case let .onXAxis(x):
  return (x, 0)
case .onYAxis(let y):
  return (0, y)
case let .noZeroCoordinate(x, y):
  return (x, y)
getValues(for: .origin)
getValues(for: .onXAxis(66.6))
getValues(for: .onYAxis(-3.5))
getValues(for: TwoDimensionalPoint.noZeroCoordinate(x: 5, y: 7))