Answer:
- enum is a keyword used to declare User defined datatype with user defined values.
enum WeekDay {
case monday
case tuesday, wednesday
case thursday
case friday
case saturday
case sunday
}
var weekDay: WeekDay = .monday
weekDay = .saturday
Answer:
- Value assigned to the enum cases. By default every enum case assigned by an int value start from 0.
- We can also change the enum case values by explicitly specifying the Type.
enum WeekDay: String {
case monday = "Mon"
case tuesday = "Tue"
case wednesday = "Wed"
case thursday = "Thu"
case friday = "Fri"
case saturday = "Sat"
case sunday = "Sun"
}
let weekDay: WeekDay = .monday
print(weekDay.rawValue) // Mon
print(WeekDay.sunday.rawValue) // Sun
Answer:
- Enum cases which are capable of holding some values are called associated values.
enum Month {
case GeneralMonth
case ExtraDaysMonth(days: Int)
}
// Associated Values
let january: Month = .ExtraDaysMonth(days: 31)
switch january {
case .GeneralMonth:
print("It is regular month")
case .ExtraDaysMonth(let days):
print("It has \(days) day")
}
// Practical Usecases
enum Error {
case NetworkError
case UnknownError(code: Int, message: String)
}
Answer:
- Enum is Value Type
Answer:
- Value type creates new instances and assigned when they passed to a method or assigned.
- Reference type just shares address of that Object when they are passed as arguments or assigned.
Answer:
- Obj-c enums doesn’t allow Raw and Associated Values
Section 3, Conditional Statement
Section 4, Enum
Section 10, static type vs dynamic type
Section 15, higher order function