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 Enums and Structs Enums Associated Values

Why's the downloadStatus a constant? Also, I don't understand the constant used under the case. success.

Isn't the downloadStatus suppose to be a variable? Also why is a constant needed in the case. Success? How is that constant valid even without assigning it.

1 Answer

Why's the downloadStatus a constant?

In the context of the example- we don't need to change its value at any point. So a constant is suitable. The documentation clarifies this:

"If a stored value in your code is not going to change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change."

Also, I don't understand the constant used under the case. success.

All we're doing here is assigning the value of .Success to a new constant called success. I think its often confusing when the examples use the same words, but we could have just as easily used:

switch downloadStatus {
case .Success(let fred):
case .Failure(let simon):
Robert Fiorendino
Robert Fiorendino
1,964 Points

I completely agree that using the same words in examples is confusing, especially for someone to whom the idea is new. Thank you for the explanation.