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

Matt Porter
Matt Porter
2,192 Points

(Enum Value)

When you assigned Day.Monday to the var Today at the end of the video...

I noticed the editor shows (Enum Value), also if you was to println(today)

It also shows (Enum Vaule) instead of Monday... which is the day you assigned to the variable today.

My question is what dose this actually mean? What would be the differences (as normally if you done this it would print out "Monday") How would you get it to print out the member of day you assigned?

Thanks!

1 Answer

The way enumerations works is designate by default an integer value to a related set of things you've defined to be things you want to group with one another.

What those integer values are during the use of your application by a user will be handled for you while you can use the alias you've set to refer to a certain item in the enumeration.

For example, Day.Monday will refer to a integer value throughout your application; instead of you remembering what that value is, roughly speaking, the compiler handles that for you.

Raw Values

Despite the default behavior, you can also assign explicit values for each item of an enumeration set; these are called raw values in Swift.

There's many ways to do this implicitly and explicitly.

Implicitly providing raw values

you can explicitly make values of an enumeration set be based on a certain number or certain data types that are easily incremental:

let Rating: Int {
  case Abysmal = 1,  Bad, Meh, Good, Awesome
}

Since integers are easily incremental, the starting value of 1 associated with Abysmal, the compiler will implicitly assign the raw values of 2, 3, 4, 5.

In general, if you use a data type that's incremental, assigning a valid value of that type to the first item and then listing each additional item separated by commas will implicitly provide raw values to each subsequent item of the enumeration set.

Explicitly providing raw values

Similar to providing raw values implicitly to an enumeration set, you can explicitly provide values to each item in your enumeration set that's valid for an explicit data type you set for that enumeration type:

enum Nickname : String {
   case Susy = "Sassy"
   case Nick = "Gopher"
  case Cymone = "Faithful"
  case Kevin = "The Beast"
}

Accessing a Raw Value

To explicitly access a raw value, regardless of how you've set one for each value in a enumeration set is straightforward: You use the rawValue method to get the such value from an item of an enumeration.

To get the raw value from an item of Rating enumeration set I've defined, you can do the following:

  let numberOfStars = Rating.Awesome.rawValue // Logs out 5

Following up

If you have any further questions about this, feel free to reply with more questions. The Enum course will later get into this; in addition to that, I implore you to read the Apple's official documentation about Enum types at least once in your early Swift career.

Edit: Used : instead of = in my explicit raw Value declarations example; was in the middle of a Sass ad CoffeeScript coding marathon before I decided to answer your question. :)

Matt Porter
Matt Porter
2,192 Points

Thanks for the wealth of information!!!

Still doesn't really answer my specific question. Im just curious as to the editor only showing (Enum Value) when you print Day.Monday.

What dose this actually mean as its not printing your Day member "Monday"... Only {(Enum Value)}

Also if you was to unwrap this in a bang operator it would remove the curly braces around the Enum Value rather than displaying Monday.

Hello again, Matt.

I actually did, but should have been more explicitly clear: When you define an enumeration set, roughly speaking, the value of each item of the enumeration set will be determined by the compiler.

Since you haven't defined a raw value for the enum (or rather an explicit starting point for rawValues to be based from), The Swift REPL won't have an unambiguous way of providing you a value for a particular item of a enumeration set Day in your case.

When you're running Playground, it conservatively doesn't show the enum value accordingly.