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

Gregory Millan
Gregory Millan
3,719 Points

Switch case

switch downloadStatus {
case .Success(let success):
    print(success)
case .Failure(let failure):
    print(failure)
}

Is switch telling Xcode to print the associated value of success & failure here? What does this exactly mean -> "(let success)"

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

As this Enum has associated values, you can "extract" them as part of the switch statement.

switch downloadStatus {
// We do not only check that this is a case of .Success, but we also
// "extract" the associated value, that is assign it to "success".
// Now you can use it within the case code block
case .Success(let success):
    print(success)
// Same here :)
case .Failure(let failure):
    print(failure)
}

This is explained in detail, including examples here: Apple Docs: Enumerations - Associated Values

Hope that helps :)