create an enum in swift

To create an enum in Swift, you can use the enum keyword followed by the name of the enumeration. Here's an example:

main.swift
enum Day {
    case sunday
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday
}
133 chars
10 lines

In this example, we've defined an enumeration called Day, which has seven cases representing each day of the week.

You can also assign raw values to enum cases if desired:

main.swift
enum Numbers: Int {
    case one = 1
    case two = 2
    case three = 3
}
75 chars
6 lines

In this example, we've defined an enumeration called Numbers, which has three cases representing the numbers 1, 2, and 3.

You can use enums in Swift to represent a fixed set of values, which can make your code easier to read and maintain.

gistlibby LogSnag