Ray Wenderlich Functions and Types Course - Part 6

Two days in a row baby! We're onto enumerations first. I want to get a couple more entries this week to finish this course, consolidate then explore more on rw.com.

Start Time - 18:50

Intro

Enums take advantage of type safety - options to choose from. They are value types.

Enums

Mostly recap here. Case syntax, raw values. Own raw values can be put in.

enum Month: Int {
    
    case january = 1, february, march, april, may, june, july, august, september, october, november, december
}



let month = Month.january

func monthsUntilChristmas(from month: Month) -> Int {
    
    Month.december.rawValue - month.rawValue

}



enum Season: String, CaseIterable {
    
    ///❄️
    case winter
    
    ///🌾
    case spring
    
    ///🌞
    case summer
    
    ///🍁
    case autumn
}


Season.allCases

Season.allCases.filter {
    $0.rawValue.first == "s"
    
}


CaseIterable is a way of allowing allCases to be shown. 

Cool, some new stuff there too! Now onto the challenge!

Challenges were hard! Last one combined closures with reduce but good stuff. 

Bonus Challenge!

Try using reduce to figure out how much money is in your coin purse. Remember to use the rawValue property!

var valueInPurse = coinPurse.reduce(0) {  (result, coin) -> Int in
    result + coin.rawValue
}

To make sense of that! You need 0 in the brackets as it is an Int total - what you are starting with. That would be "" for String. Then you have two values in - result and coin. These are the values created to then interact with. The bit in the actual code - result + coin.rawValue is.... The result is 0 at the moment and adding to it is the raw value of each coin. 

Finish Time - 19:32 (42 minutes total)

A good little session! Enums are definitely more complex at this level, which is good. I've got the essential parts of it. Next time, switch statements!


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)