Programming in Swift: Functions & Types

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

Part 3: Enumerations

26. Associated Values

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

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: 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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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))