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 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):
    println(fred)
case .Failure(let simon):
    println(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.