Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

properties-swift

Properties

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Properties: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Properties in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Properties in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift properties include stored, computed, lazy, and property observers—unifying fields and accessors without Java-style getter/setter boilerplate.

Stored and computed

struct Rect {
    var width: Double
    var height: Double
    var area: Double { width * height }
}

Observers and lazy

var score: Int = 0 {
    willSet { print("changing to \(newValue)") }
    didSet { print("was \(oldValue)") }
}

lazy var loader = ExpensiveLoader()

lazy delays initialization until first access—common in reference types loading resources.

Important interview questions and answers

  1. Q: Computed property vs method?
    A: Computed properties for side-effect-free derived values; methods when work is expensive or has parameters beyond self.
  2. Q: lazy on structs?
    A: Allowed but mutating—lazy stored properties require mutating access on struct methods because they change storage.

Self-check

  1. What triggers willSet and didSet?
  2. When is a lazy property initialized?

Pitfall: lazy on struct properties requires mutating access because initialization mutates storage.

Interview prep

Computed vs method?

Computed for side-effect-free derived values; methods when parameterized work.

lazy when?

Deferred expensive initialization until first access.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • lazy property?
  • didSet use case?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump