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 Enumerations and Optionals in Swift Objects and Optionals Initializing With Raw Values

Lana Wong
Lana Wong
3,968 Points

Why do you have to declare a constant and then assign that constant to the initializer?

HI, In this video, Pasan created a constant called statusCode and assigned a value of 200. Is that constant really necessary? In Xcode, I was able to put 200 as the rawValue , so I was just wondering if Pasan did an extra step.

2 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

Hi,

You don't have to declare a constant. Forgive me if I'm not 100% accurate...I haven't watched that video in a while and I'm busy watching other videos, but I did keep my notes on it.

I think that Pasan created a constant to use instead of the actual value (i.e. 200)...

  1. To condition us the concept of storing data into constants/variables with meaningful names to improve code readability. For example, hypothetically, when you see the number 200 in code, you're probably not going to immediately know the purpose of that number unless you view the code that surrounds that number; however, if you see a constant named "statusCode" instead a number, you probably would get the purpose of the code that you are viewing much faster than just by looking at a number--when you see the constant's name, you automatically understand that it's a status code for something, but when you see the number 200...it's just the number 200.

  2. Based on my notes...after Pasan displayed...

enum HTTPStatusCode: Int
{
    case success = 200, forbidden = 403, unauthorized = 401, notFound = 404
}

let statusCode = 200

....I think he then displayed...

let httpStatusCode = HTTPStatusCode(rawValue: statusCode) // enum raw Values are optional types

if let httpStatusCode = HTTPStatusCode(rawValue: statusCode) // used if let to unwrap the optional value
{
    print(httpStatusCode)
}

Perhaps Pasan wanted to illustrate how httpStatusCode (the constant holding the enum instance) is an optional even though statusCode (which is not an optional) was passed in to the enum when the constant was created. I assume this because my notes shows Pasan unwrapping the enum value afterward...

So after the "global" httpStatusCode constant was created, Pasan then coded (based on my notes) the if let with a "local" httpStatusCode constant, along with the enum (an optional type) that was passed an non-optional value (i.e. statusCode), in order to unwrap the enum value.

The print method was successful because the local constant of the if let got assigned an optional value that wasn't nil in order for that value to be unwrapped and printed out--the local httpStatusCode has to accept an optional value that is not nil in order to be successful because the constant is part of an if let statement--once the if let constant accepts a value, that value converts to a type that's not optional (a guard let statement can unwrap too, and an unwrapped value can exist outside the scope of a guard let, but the compiler expects a return keyword (or a throw keyword), for a guard let to work. Thus, a guard let must be in a function for it to work since those keywords work in functions...hmm...now I see why Pasan was using functions with an imaginary dictionary to teach us about guard lets...that imaginary friend dictionary as a parameter for the function confused me at first...). The above code is the same as...

let httpStatusCode = HTTPStatusCode(rawValue: statusCode) // enum raw Values are optional types

if let httpStatusCode = httpStatusCode// used if let to unwrap the optional value
{
    print(httpStatusCode)
}

I just assigned the global constant, to the local constant of the if let to unwrap. Both constants are different, one is global, one is local, one is an optional, one is not an optional, both have the same name.

Arman Arutyunov
Arman Arutyunov
21,900 Points

No, Pasan created a constant with hardcoded value not without a reason. He tries to show you the reverse procedure of getting information from an enum case. Knowing some kind of status code with some kind of value doesn't mean anything to you if you didn't learn by heart all the internet status codes. This shows you that you can pass a value to the enum and get it's meaning back (meaning that you will be able to understand). Here it is a "success" case that Pasan stored in a new constant.

Let's assume you get this 200 status code from a server callback but you don't know what it means. With such an enum you can easily associate code with an enum case and then work with comfort. What could you do afterwards is something like this

switch httpStatusCode {
    case .success: //continue working with the server
    case .forbidden: //deal with it or cry :)
    case .unauthorized: //tell the user that he passed wrong credentials or something
    case .notFound: //double check your URL
}