Intermediate Swift 3 - Part 9: Enumerations | Ray Wenderlich

In this Intermediate Swift 3 video tutorial you'll learn how to work with enumerations in Swift to make your code safer and easier to read.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3536-intermediate-swift-3/lessons/9

Hi Brian,

I’ve followed both the basic and intermediate Swift 3 video tutorials and they are great ! Thanks.

I have to say I have not quite understood the use of Associated Values. I’ve just done the challenge and thought the second part would help me understand this point, but not really. Could you please elaborate about Associated Values both in the video and Challenge? Associated Values seem like a very new feature for enums especially in comparison to other programming languages. In which case one would want to use Associated Values and a companion function over using other constructs and regular functions?

Thanks in advance.

Joss.

@vegetarianzombie,

Enjoyed the intermediate video, it was good to put reason behind some of the things that I’ve been doing. on the Enumeration, I thought I understood what I was doing until I tried calling the routine in the challenge, monthsUntilWinterBreak(from month: Month2) → Int, I used monthsUntilWinterBreak(month: 6) and got the message “Cannot convert value of type ‘Int’ to expected argument type ‘Month’” I tried passing in an Int, and then typecast it: var mm: Month = (Month) month didn’t work. Confused. How would this be done?

Bob

I think I figured it out using the init method of the enum?

let mi = 4

let mm: Month! = Month.init(rawValue: mi)
monthsUntilWinterBreak(month: mm)

Joss,

I totally agree with your post. I’ve now completed all the Swift 3 basic and intermediate tutorials. I left each video with a great understanding of the concept that is being explained. This is the first topic that has left me confused at the end of the video.

There is so much happening here that I will have to go to another source to figure out Enumerations and Associated Values. 10 minutes is just too fast for the concepts presented. That last part of the video I am holding my heading and screaming “WHAT IS HAPPENING!!” :grinning:

I would suggest breaking Enumerations into two parts with the second part focusing on associated values.

I found the slow read through Apple’s “The Swift Programming Language (Swift 3.1)” section on Enumerations and practicing each example really helped in understanding. I would recommend this to anyone and then head back to the video (which is good - just fast!).

Another note: I found the solution to the Ub3r H4ck3r Challenge to be a little clunky. Since we have learned about extensions, here is a far more l33t solution:

extension Month {
    func monthsUntilWinterBreak() -> Int {
        return Month.december.rawValue - self.rawValue
    }
}

That way you can simply call the function of the original Month enum without worrying about introducing a second and confusing enum type. It operates like this:

var theMonth = Month.may
theMonth.monthsUntilWinterBreak()

The example above returns 7.

And one final note, the solution gives the enum the name “Months”; according to the Apple Swift Guide on Enumerations this is not a best practice. The guide states “Give enumeration types singular rather than plural names so that they read as self evident”.

1 Like

hi. would have been better if the exercise also covered raw and associated values. even if these are not greatly discussed, exercises would have helped in understanding the topic. thanks!