Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
}