Enums
Enums are frequently used in Swift to create custom data types that have a predefined set of possible values to select from. Enums serve to make code more readable and maintainable, and also provide compile-time checking for parameters and value assignments which yield higher quality, more robust code.
Many languages provide built-in enum features, and Swift's implementation of the enum is very similar to other languages. Swift does have some unique enum features, which we'll cover in this section.
Basic Enum Syntax
Consider the following code, which creates and uses a basic enum:
enum DayOfWeek { case monday, tuesday, wednesday, thursday, friday } var today = DayOfWeek.wednesday if today == .friday { print("Today is Friday") } else { print("Today ...
Get Beginning Swift now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.