Enum & Optionals Flashcards
What’s the purpose of using an enum?
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 }
Can you incorporate a function into an enum?
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()
What’s the purpose of using optionals?
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!
What is the purpose of using a guard statement?
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 }
Why would you want to create an enum with default raw values?
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 }
When we create an optional value in a class, it is automatically initialized to what? Example var buildingName: String?
Nil
What is the rawValue of the third item of an emun with type Int?
3
What is the rawValue of the fourth item of enum with type String that is called post?
“Post”
What is the syntax for a failable initializer?
Init?
init?(dictionary: [String: String]) {
return nil
}
What is the purpose of optional chaining?
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
Do you need an initializer for classes with stored properties set up as optional variables or default types?
No you do not. It is automatically initialized as nil
See Classes Address, Residence , Person
TF: only an optional type can be set to nil
True
When an enum member has an associated value, how do you access it?
By binding it to a local constant
See the colorComponent enum example
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.
True
When an enum has a default value for a case, what is that value called? i.e. case tab = “/t”
A raw value