iOS Interview Question (Part 2)
1. Is default case is necessary in Swift’s Switch case?
No, the default
case in Swift's switch
statement is not necessary. It is optional and can be omitted if you don't need a fallback option for unmatched cases.
2. What is Fall-through keyword in swift?
It is used with Switch. Moves to the next case without checking its condition.
Example
let number = 2
switch number {
case 1:
print("Case 1")
case 2:
print("Case 2")
fallthrough // Move to the next case
case 3:
print("Case 3")
default:
print("Default case")
}
// output
Case 2
Case 3
Enum:
An enum
(short for enumeration) in Swift is a custom data type that groups related values together. It helps make code more readable, type-safe, and error-free.
1️⃣ Basic Enum
Enums define a set of possible values (cases).
enum Day {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
var today: Day = .monday
print(today) // Output: monday
✅ No need for raw values unless required
✅ Strongly typed: You can’t assign today = "Monday"
(unlike in Objective-C)
2️⃣ Enum with Raw Values
You can assign raw values to cases (e.g., Int
, String
).
enum StatusCode: Int {
case success = 200
case notFound = 404
case serverError = 500
}
let status = StatusCode.success
print(status.rawValue) // Output: 200
✅ Use .rawValue
to access the underlying value.
3️⃣ Enum with Associated Values
Each case can hold different types of associated values.
enum Payment {
case cash(amount: Int)
case creditCard(number: String, cvv: Int)
}
let myPayment = Payment.creditCard(number: "1234-5678-9876", cvv: 123)
switch myPayment {
case .cash(let amount):
print("Paid \(amount) in cash")
case .creditCard(let number, let cvv):
print("Paid using card \(number) with CVV \(cvv)")
}
✅ Each case can hold different types of data.
4️⃣ Enum with Computed Properties
Enums can have computed properties for additional logic.
enum Temperature {
case hot, cold
var description: String {
switch self {
case .hot: return "It's 🔥 hot!"
case .cold: return "It's ❄️ cold!"
}
}
}
print(Temperature.hot.description) // Output: It's 🔥 hot!
✅ Adds custom behavior to enum cases.
5️⃣ Enum with Methods
Enums can include functions.
enum NetworkStatus {
case online, offline
func message() -> String {
switch self {
case .online: return "Connected ✅"
case .offline: return "No internet ❌"
}
}
}
print(NetworkStatus.online.message()) // Output: Connected ✅
✅ Methods make enums more powerful and reusable.
6️⃣ Enum with Mutating Methods
Enums with mutating
methods can modify self
.
enum LightSwitch {
case on, off
mutating func toggle() {
self = (self == .on) ? .off : .on
}
}
var light = LightSwitch.off
light.toggle()
print(light) // Output: on
✅ mutating
allows modifying the enum within a method.
7️⃣ Enum with CaseIterable
Get all enum cases using CaseIterable
.
enum Beverage: CaseIterable {
case coffee, tea, juice
}
print(Beverage.allCases.count) // Output: 3
Please like and follow if you really like my article, it motivates me to continue….