Skip to content

Latest commit

 

History

History
145 lines (93 loc) · 3.27 KB

File metadata and controls

145 lines (93 loc) · 3.27 KB

Enum Questions 1 ~ 6

1) What is Enum?

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

2) What is rawValue in enum?

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

3) What is Associated values in Swift?

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)
}

4) Is enum value type or reference type?

Answer:

  • Enum is Value Type

5) Value Type vs Reference 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.

6) What is the difference between Swift Enum and Objective-C Enum?

Answer:

  • Obj-c enums doesn’t allow Raw and Associated Values

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C