Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

extensions-swift

Extensions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

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

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

You will apply Extensions 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.

Extensions add methods, computed properties, and protocol conformances to existing types—even types you do not own, like String or Foundation types.

Syntax

extension String {
    var isEmailLike: Bool { contains("@") }
}

extension Int {
    func squared() -> Int { self * self }
}

Organizing code

Split large types across files with extensions grouped by protocol conformance—cleaner than monolithic class files.

Important interview questions and answers

  1. Q: Extension vs subclass?
    A: Extensions cannot add stored properties or override; they augment behavior without changing inheritance.
  2. Q: Can extensions add protocol conformance?
    A: Yes—retroactive modeling lets you adapt third-party types to your protocols.

Self-check

  1. Can extensions add stored properties?
  2. Can you extend types from other modules?

Pitfall: Extensions cannot add stored properties—only methods and computed properties.

Interview prep

Stored properties in extensions?

No—only methods and computed properties.

Retroactive modeling?

Extend third-party types to conform to your protocols.

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

  • Extend struct?
  • Retroactive conformance?

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