Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 3: Enumerations

22. Challenge: Enumerations

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 21. Enumerations Next episode: 23. Switch Statements

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 22. Challenge: Enumerations

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 22. Challenge: Enumerations

I’ve got a challenge to help you practice using enumerations! You’ll find everything you need on page 3 of the Playground for this part of the course, including some starter code! Pause the video, try the challenges on your own, and then come back to compare your work to mine.

Hey! Welcome back. Let’s go over these challenges. First up - I gave you some starter code that was creating SwiftUI images using this systemName initializer. That’s making use of SF Symbols, which is a set of vector images that you can use in your apps.

The trouble is, you have to make sure you spell the name of the symbol correctly. If you tried to run this playground and look at the images,

you’d see that “heart” and “spade” work, but “club” and “diamond” are just blank. Well, that’s because I misspelled their symbol names. This doesn’t cause a crash at all, I just don’t get the images I wanted!

So! To prevent this from happening, I asked you to use an enumeration.

enum Suit

and to give it a String raw value type

enum Suit: String {

}

Then I need one case for each image. I’ll called them heart, club, spade, and diamond.

case heart
case club
case spade
case diamond

While this would give us some strings to work with, they aren’t the strings we need. We still need all this other stuff to get the right image.

So I’ll manually set the raw value for each case, and make sure everything gets spelled correctly along the way.

  case heart = "suit.heart.fill"
  case club = "suit.club.fill"
  case spade = "suit.spade.fill"
  case diamond = "suit.diamond.fill"

Now I can replace all of these strings with the raw value of an enumeration case, which I will always know if I’ve spelled correctly because the compiler will tell me so. I also get auto-complete working for me!

let heartIcon = Image(systemName: 😺Suit.heart.rawValue).resizable()
let clubIcon = Image(systemName: 😺Suit.club.rawValue).resizable()
let spadeIcon = Image(systemName: 😺Suit.spade.rawValue).resizable()
let diamondIcon = Image(systemName: 😺Suit.diamond.rawValue).resizable()

Now when I run the playground, I get all of the images I want!

Onto Challenge 2! Integer raw values don’t have to be incremental! So, create an enumeration to represent different types of coins

enum Coin

I’m going to model American currency and use Double as a raw value type

enum Coin: Double {

}

For the cases, I’ll use a penny, nickel, dime, and quarter - and set them to their respective values

  case penny = 0.01
  case nickel = 0.05
  case dime = 0.10
  case quarter = 0.25

Now I can make my coin purse!

let coinPurse: [Coin]

I explicitly typed it so that in the array itself, I can just use dot penny, dot dime, and so on.

 = [.penny, .penny, .dime, .quarter, .quarter, .nickel, .penny, .quarter]

Then there was a bonus challenge to use reduce to figure out how much money is in the coin purse. I’ll make a new constant called total, and call reduce on my coinPurse

let total = coinPurse.reduce(

The value I want to start with is 0, in double form

.reduce(0.0) {

And I’ll call these parameters result and coin, and the closure will return a double.

{ (result, coin) -> Double in

Now I just need to add the result, which starts at 0, and the raw value of each coin.

result + coin.rawValue

And then run the playground! I’ve got exactly 93 cents!