Enum & Optionals Flashcards

1
Q

What’s the purpose of using an enum?

A

An enum is great to use for a group of related values that defines a common type.

So let’s say we wanted to do something related to days of the week, Day is the common type with the different days serving as values or cases

enum Day {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday
    case sunday
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you incorporate a function into an enum?

A

Yes.

enum ColorComponent {
    case rgb(Float, Float, Float, Float)
    case hsb(CGFloat, CGFloat, CGFloat, CGFloat)
    func color() -> UIColor {
        switch self {
        case .rgb(let red, let green, let blue, let alpha):
            return UIColor(colorLiteralRed: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
        case .hsb(let hue, let saturation, let brightness, let alpha):
            return UIColor(hue: hue/360.0, saturation: saturation/100.0, brightness: brightness/100.0, alpha: alpha)
        }
    }
}

ColorComponent.rgb(230.0, 26.0, 193.0, 1).color()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the purpose of using optionals?

A

It’s a great way for us to know that the specific item may or may not have a value. Such as:

struct Friend {
    var firstName: String
    var middleName: String?
    var lastName: String
  }

Not everyone has a middle name!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of using a guard statement?

A

An early exit or guard statement is a way of checking for success cases first. You deal with errors up front and exit the scope.

You use them for non optional values?

guard let name = friendDictionary["name"], let age = friendDictionary["age"] else {
        return nil
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why would you want to create an enum with default raw values?

A

Those cases may have values that are not going to change. Such as:

enum Coin: Double {
    case penny = 0.01
    case nickel = 0.05
    case dime = 0.10
    case quarter = 0.25
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When we create an optional value in a class, it is automatically initialized to what? Example var buildingName: String?

A

Nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the rawValue of the third item of an emun with type Int?

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the rawValue of the fourth item of enum with type String that is called post?

A

“Post”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for a failable initializer?

A

Init?
init?(dictionary: [String: String]) {
return nil
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of optional chaining?

A
It helps us get the value of something without doing a bunch of if lets to check if the value might be nil 
let buildingNumber = susan.residence?.address?.buildingNumber
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Do you need an initializer for classes with stored properties set up as optional variables or default types?

A

No you do not. It is automatically initialized as nil

See Classes Address, Residence , Person

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

TF: only an optional type can be set to nil

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When an enum member has an associated value, how do you access it?

A

By binding it to a local constant

See the colorComponent enum example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TF: Raw value initializers accept any value that conforms to the type requirements. An optional value allows the initializer to return nil in case there is no match.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When an enum has a default value for a case, what is that value called? i.e. case tab = “/t”

A

A raw value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When an enum has a value type, what is that value called? I.e. case qrCode(String)

A

An associated value

17
Q

What is the nil-coalescing operator?

A

a ?? b

This unwraps the optional a if it has a value or returns a default value of b if a is nil

18
Q

TF: You cannot include a control transfer statement in the else clause of a guard statement

A

False

19
Q

TF: Enums are well suited for use with switch statements because the compiler can deduce exhaustiveness

A

True

20
Q

TF: Associated values allow us to provide default values to our enumeration members

A

False

21
Q
What is the value of `c` given the code below? 
var a: Int? = nil
let b = 2

let c = a ?? b

A

2

22
Q

TF: Enums can do everything structs and classes can do including contain stored properties

A

False

23
Q

TF: In an optional chaining expression, if one part of the expression fails, an error is raised

A

False