Enumerations and Optionals: Intro to Enumerations (Part 2)

Continuing with Enums...really enjoying these so far!

Associated Enums

Pasan mentioned a 'wrapper' object...apparently we will find out more! In the example of a colour component enum, we need to provide associated enums...Adding in four float values in brackets - these four represent the rgb colour case.

enum ColourCoomponent {
    case rgb (Float, Float, Float, Float)
    case hsb
    

}

The same needs to be done for the hsb case. So for each Enum member, there are parameters involved. Accessing them with the dot notation means they are properties?

enum ColourCoomponent {
    case rgb (Float, Float, Float, Float)
    case hsb (Float, Float, Float, Float)
    
}

let colour = ColourCoomponent.hsb(55, 77, 91, 109)

Methods on Enumerations

A point Pasan makes straight away is that an enum is not really an object - we don't create instances of them. But it can have instance methods, like objects do. So, a func is used within the enum code...

func colour() -> UIColor {
        switch.self {
        case .rgb(let red, let green, let blue, let alpha):
            return UIColor(color
        }

Now I'm stopping the code there! There's something with my newer version of Xcode that doesn't allow the same options. No biggie. What I'll have to do is, after this video, seek out some other information about Enumerations, just to ensure I have not missed anything! It was a very specific example with Treehouse and fraught with errors due to newer versions of Xcode etc. 

A key point is that the associated values (what types are used in brackets for different cases) can vary for each case. E.g. I could have a String one and two Ints for the next one etc. 

enum BarButton {
    case done(title: String)
    case edit(title: String)
    
    func button() -> UIBarButtonItem {
        switch self {
        case .done: return UIBarButtonItem(title: "Done", style: .done, target: nil, action: nil)
        case .edit: return UIBarButtonItem(title: "Edit", style: .plain, target: nil, action: nil)
        }
    }
}

let done = BarButton.done(title: "Save")

let button = done.button()

That was bloody hard! I got the idea of the func and switch bit, but was flummoxed by this UIBarButtonItem. Next time, I will be careful when there is a class, and check beforehand what the initialiser values are! Hey, at least I'm getting my head around that terminology!

So, before I finish, I just want a little more practice with Enums...

Comprehensive Guide to Swift

Ibram (course instructor on Udemy) explains that associated values are better than using the 'raw' values. In his example, it is basically a tuple - two or more values used here. 

enum Attacks {
    
    case punch(Int, Int)
    case uppercut(Int, Int)
    case kick(Int, Int)
    case defend(String)
}

I've gone a little further than Ibram, who put the labels in a comment...

enum Attacks {
    
    case punch(cost: Int, damage: Int)
    case uppercut(Int, Int)
    case kick(Int, Int)
    case defend(String)
}
var currentAttack = Attacks.punch(cost: 2, damage: 10)

Here I have given parameter names. Cool! Nice to be ahead of the game and go a little further. He also put 'punch' in capitals - not good syntax. The next part of what he does doesn't really match up to the newer Xcode - putting in values in the cases...

enum Months: Int {
    
    case Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
}

Apologies Ibram - you were right! I had missed the declaration of type Int. So Xcode figures out that Feb is 2, Mar is 3 etc. Handy!

So something new there - if you are JUST using your enum for one specific type. In reality, I'd probably use a variety, by using the tuples. 

Introduction to Enumerations is now complete! I really like the functionality of this code and how you can combine methods. I'm sure that I will come back to this. 

Looking at the Udemy courses I have bought, there are 23 in total! Now only 2 of those got properly complete! What this tells me is that at some point I'm going to do a big focus on these, as there is a lot of real-life app making tied in with them. What I'll do is complete the current Treehouse ones and then put that subscription on pause, coming back to it perhaps at the Intermediate level. I'm still on the Beginner track, so that will be the perfect time to pause my payment, go through some of the Udemy ones, then resume at a later time. This makes sense as there are 2 more courses after the one I am on. That will be the perfect time to then switch to the Udemy courses. Exciting!



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)