0Pricing
Swift Academy · Lesson

Properties & Subscripts

Use lazy stored properties, get/set computed properties, property observers, and subscripts for custom indexing.

Properties & Subscripts is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

This lesson covers advanced properties: lazy, computed with get/set, observers, plus custom subscripts.

lazy property

lazy properties are created only on first access. Useful for expensive work.

struct DataLoader {
    lazy var data: [Int] = {
        print("Loading...")
        return [1,2,3]
    }()
}
var loader = DataLoader()
print("before access")
print(loader.data)

Computed get/set

Computed properties use get/set blocks to calculate or update values.

struct Circle {
    var radius: Double
    var diameter: Double {
        get { return radius * 2 }
        set { radius = newValue / 2 }
    }
}
var c = Circle(radius: 5)
print(c.diameter)  // 10
c.diameter = 20
print(c.radius)    // 10

Observers

Use willSet and didSet to react to property changes.

class Account {
    var balance: Double = 0 {
        willSet { print("Will set to", newValue) }
        didSet  { print("Did set from", oldValue) }
    }
}
var a = Account()
a.balance = 50

Subscript

Subscripts let you define custom access with bracket syntax.

struct TimesTable {
    let multiplier: Int
    subscript(i: Int) -> Int {
        return multiplier * i
    }
}
let three = TimesTable(multiplier: 3)
print(three[4])   // 12

Multi-parameter subscript

Subscripts can take multiple parameters, e.g., a matrix with row and column indices.

struct Matrix {
    var grid: [[Int]]
    subscript(row: Int, col: Int) -> Int {
        return grid[row][col]
    }
}
let m = Matrix(grid:[[1,2],[3,4]])
print(m[1,0])   // 3

lazy Q

Quick check: What does the lazy keyword do?

Recap

Recap: Use lazy for deferred init, get/set for computed props, willSet/didSet for observing, and subscripts for custom access.

Frequently asked questions

Is the “Properties & Subscripts” lesson free?

Yes — the full text of “Properties & Subscripts” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Properties & Subscripts”?

Use lazy stored properties, get/set computed properties, property observers, and subscripts for custom indexing. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Properties & Subscripts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Properties & Subscripts
  2. lazy & property observers
  3. Computed properties with get/set
  4. Subscripts for custom indexing
← Back to Swift Academy