why Swift needs enums at all!

An enum is simply a nice name for a value. We can make an enum called Direction with cases for north, south, east, and west, and refer to those in our code. Sure, we could have used an integer instead, in which case we’d refer to 1, 2, 3, and 4, but could you really remember what 3 meant? And what if you typed 5 by mistake?

So, enums are a way of us saying Direction.north to mean something specific and safe. If we had written Direction.thatWay and no such case existed

Other examples of enums:

1) Enums can be used to store Error types :

There are only a fixed number of errors you can issue, so they make a great choice for enums.

2) Genres of movies

3) Days of the week

Enum Associated Values:

As well as storing a simple value, enums can also store associated values attached to each case.

One of the most powerful features of Swift’s enums is their ability to store one or more associated values — extra pieces of information that describe the enum case in more detail.

This lets you attach additional information to your enums so they can represent more data.

For example, we might define an enum that stores various kinds of activities:

enum Activity {

case bored

case running

case talking

case singing

}

That lets us say that someone is talking, but we don’t know what they talking about, or we can know that someone is running, but we don’t know where they are running to.

Enum associated values let us add those additional details:

enum Activity {

case bored

case running(destination: String)

case talking(topic: String)

case singing(volume: Int)

}

let talking = Activity.talking(topic: “football”)

ios developer