ProgrammingMobile Developer

How do switch/enum work in Swift, what are their features and how do they differ from their counterparts in other languages?

Pass interviews with Hintsage AI assistant

Answer.

In Swift, the switch and enum constructs are implemented with powerful pattern matching capabilities and strict type safety, which distinguishes them from most counterparts in other languages.

Background

In C and C++, as well as in Objective-C, enumerations are simply a set of integer values, and switch statements compare values for matches. In Swift, enumerations (enum) are much more powerful — they support associated values, computed properties, and methods. The switch statement supports pattern matching, guards against incomplete branches, and works with ranges, tuples, optionals, and much more.

Problem

In traditional languages, switch statements often lead to errors related to the absence of exhaustiveness (comprehensive consideration of all cases), type errors, and the inability to safely store additional data in enum cases. This leads to runtime errors rather than compile-time errors.

Solution

In Swift, the switch statement requires full handling of all cases unless the enumeration is explicitly marked as @unknown default. Associated values allow for elegant storage of additional information in enum cases. For example:

enum NetworkResult { case success(Int) case failure(String) } func handle(result: NetworkResult) { switch result { case .success(let code): print("Success with code: \(code)") case .failure(let error): print("Failure with error: \(error)") } }

Key features:

  • Enums can have associated values/data
  • The switch mechanism requires complete coverage of all cases (exhaustive matching)
  • Swift supports pattern matching with tuples, ranges, optionals, not just enums

Tricky questions.

Is it always necessary to write default in switch for enum?

No, if all enumeration cases are explicitly covered, default is not needed. Moreover, using default is not recommended unless necessary — the compiler may not warn about new enum cases being added.

Can fallthrough be used for automatic transitions between cases?

Yes, the keyword fallthrough is available, but it should be used carefully. It does not carry associated values, and such transitions are rarely seen in real Swift practice.

switch number { case 1: print("one") fallthrough case 2: print("or two") default: print("other") }

Can an enum in Swift be compared by rawValue if it has associated values?

No. Only enumerations without associated values and with explicitly specified rawValue can be initialized and compared through rawValue.

Common mistakes and anti-patterns

  • Adding default in a switch statement for an enum for "safety" — this hides the emergence of new cases.
  • Using enums with a large number of cases to store complex data when it is better to use a struct.
  • Not using pattern matching and ignoring associated values.

Real-life example

Negative case

A developer added a default-case in a switch statement for the enum NetworkResult, which led to the logic not being updated upon adding a new case, and the program "silently" malfunctioned.

Pros: No compiler warnings when adding cases.

Cons: Errors manifest during application runtime, logic is not automatically updated.

Positive case

An employee refused to use default in a switch statement for an enum, which allowed the compiler to identify the lack of handling of new cases after their addition.

Pros: Errors are detected at compile time, application behavior becomes more predictable.

Cons: More code is required with a large number of cases, but we gain reliability.