Thor
Thor

Reputation: 10068

in swift enum, when to use raw value and when to use associated value

Someone on StackOverflow has already provided an excellent answer to the question: what is the difference between raw value and associated value in swift.

Difference between associated and raw values in swift enumerations

However, the question did not include information on when to use raw value and when to associated value. This is the part I'm a little confused about. It would be greatly if someone can explain it to everyone. I think this is a problem that a lot of beginners to swift would have as well.

Upvotes: 3

Views: 1312

Answers (2)

Earl Grey
Earl Grey

Reputation: 7466

You pick one that logically matches your business case/requirement/feature/model BETTER.

You should look at the anatomy of swift Enum and try to find cases/situations/options in real world which match one of the two Enum variants. I'll give you an example for both.

RAW VALUE

enum CountryAcronyms: String {

    case UnitedKingdom  = "UK"
    case Germany        = "DE"
    case Australia      = "AU"
}

Here you are dealing with cases are all the same category of things which is Country and each country can be represented by a single one acronym, that is of type String. The important fact here is that the underlying type for acronyms all across is String. So "RawValue"..in other words is when you have ONE AND THE SAME underlying type chosen to represent EACH case. The when you want to extract the underlying value of the underlying type, you use the rawValue accessor.

ASSOCIATED VALUE

enum Trip {

    case Abroad(Airplane, Taxi, Foot)
    case Grandma(Tube, Foot)
    case McDonalds(Car)
    case MountEverest(Ski, Foot)
}  

Here we have a set of cases and each represent also one thing - a Trip, but the associated types in this enum represent THE MEANS (this is what we chose..that's it! Perhaps there is a business case or a design ..or simply customer wants it..) and Since the means DIFFER for each case, we associate a unique type (in this case tuples with 1 or more types) that is able to represent the means. Since we wanted to represent something like this we couldn't chose the previous Enum approach because we would have no way to express various means.

Upvotes: 6

matt
matt

Reputation: 535850

If you want a case of an enum to stand for one simple constant literal value, use a raw value.

enum PepBoy : String {
    case Manny = "Manny"
    case Moe = "Moe"
    case Jack = "Jack"
}

If you want a case of an enum to carry arbitrary value(s) of a predefined type, use an associated type.

enum Error : ErrorType {
    case Number(Int)
    case Message(String)
}

Usage examples:

let pb : PepBoy = .Manny
print(pb.rawValue) // "Manny" [and only ever "Manny", if the case is Manny]

do {
    let e = Error.Message("You screwed up") // attach a message [any message]
    throw e
} catch Error.Message(let whatHappened) { // retrieve the message
    print(whatHappened) // "You screwed up"
} catch {
}

Upvotes: 6

Related Questions