Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Enumerations and Optionals Objects and Optionals Recap: Enumerations and Optionals

Emily Kolar
Emily Kolar
2,787 Points

Can someone recap the difference between Associated value and Raw Value in an enum?

I am getting confused on the difference.

1 Answer

Tassia Castro
seal-mask
.a{fill-rule:evenodd;}techdegree
Tassia Castro
iOS Development Techdegree Student 9,170 Points

Hey Emily,

Associated value is related to a case in an Enum and each case can have a different associated value

enum Barcode {
    case upc(Int, Int, Int, Int) // The associated value here is a Tuple
    case qrCode(String) // The associated value here is a String
}

"Associated Values shows how cases of an enumeration can declare that they store associated values of different types."

However the Raw value is related to the Enum in general and you can define only one type that all the cases in the Enum will have.

"As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type."

enum ASCIIControlCharacter: Character {
    case tab = "\t" // The associated value here is a Character
    case lineFeed = "\n" // The associated value here is a Character 
    case carriageReturn = "\r" // The associated value here is a Character
}