← Back to list

Swift Rewind : The Fundamentals of Data Modeling

Season 3— Episode 1: Enums in Action — The Power of Finite Choices

Krishna Panchal · 2026-02-14 15:01 · 0 claps · 2.0 min read
#swiftenum #swift-programming #swift-rewind #enumeration #enums-in-swift
Open on Medium ↗
Wiki topics: 💻 · Programming 📱 · Mobile Development 🔧 · Data Engineering

Swift Rewind : The Fundamentals of Data Modeling

Season 3— Episode 1: Enums in Action — The Power of Finite Choices

So far, we’ve been working with Swift’s built-in types like Int, String, Array, and Dictionary. But Swift also allows us to create our own types. With these new season, in this episode, we focus on Enumerations (Enums).

An enum lets you define a type that can have one of a fixed set of values. We’ve used enums in other languages, but Swift enums come with powerful and advanced capabilities.

// Basic Syntax
enum TextAlignment {
    case left
    case right
    case center
}

Declared using the enum keyword. Must contain at least one caseBecomes a new type (like Int or String) If you know a particular variable as enum type, you can omit the type when assigning new value to variable.

// Creating and Using an Enum
var alignment: TextAlignment = TextAlignment.left

// With type inference:
var alignment = TextAlignment.left
alignment = .right

// Omit when comparing values:
if alignment == .right {
    print("Right align the text")
}

Handling Enums with switch

Although enum values can be checked using if, they are usually handled with a switch statement.

var alignment = TextAlignment.left

switch alignment {
case .left:
    print("left aligned")
case .right:
    print("right aligned")
case .center:
    print("center aligned")
// You can use default, but it's not recommended:
default:
    print("center aligned")
}

Swift requires switch statements to be exhaustive — meaning all possible enum cases must be handled. With enums, the compiler already knows all possible cases. If you include every case, no default is required. This works — but it’s not future-proof.

Why to avoid default? — If you later update your enum: Your program will still run — but may produce incorrect results if default handles new cases unintentionally.

enum TextAlignment {
    case left
    case right
    case center
    case justify
}

// Instead, explicitly list every case:
switch alignment {
case .left:
    print("left aligned")
case .right:
    print("right aligned")
case .center:
    print("center aligned")
case .justify:
    print("justified")
}

Now, if you add a new case in the future, the compiler will immediately show an error until you handle it. This makes your code safer and more maintainable.

Naming Conventions

PascalCasedType — If multiple words are needed, capitalize the first letter of each word (this is called Pascal case)

CamelCasedType — The names of variables, functions, and enum cases use a similar case structure but with a lowercase first letter (called camel case)

⭐️ You’ve just completed Episode 1 of Season 3:

In this episode, we explored Enumerations in Swift — how to define them, use type inference, and handle them safely with exhaustive switch statements.

Stay tuned — more calm, clear, and practical Swift concepts are coming next. Let’s keep rewinding Swift, one clean concept at a time.

💬 Found this helpful? Give this Medium post a like and share it with a fellow iOS developer revisiting Swift fundamentals.


메타데이터
post_id
b3513e581cab
slug
swift-rewind-the-fundamentals-of-data-modeling-b3513e581cab
url
https://medium.com/@krishhna.ios/swift-rewind-the-fundamentals-of-data-modeling-b3513e581cab
canonical_url
https://medium.com/@krishhna.ios/swift-rewind-the-fundamentals-of-data-modeling-b3513e581cab
author_url
https://medium.com/@krishhna.ios
status
ok
fetched_at
2026-06-23 17:05:31