0Pricing
Swift Academy · Lesson

Computed Properties in Structs

Deriving values with get-only and get/set computed properties.

Computed Properties in Structs is a free Swift Academy lesson on CoddyKit — lesson 3 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.

Welcome

Computed properties don't store a value — they compute and return one on demand. They look like properties to callers but run code under the hood.

Read-Only Computed Property

```swift struct Circle { var radius: Double var area: Double { .pi * radius * radius } var circumference: Double { 2 * .pi * radius } } var c = Circle(radius: 5) print(c.area) // 78.53... ```

get and set

```swift struct Temperature { var celsius: Double var fahrenheit: Double { get { celsius * 9/5 + 32 } set { celsius = (newValue - 32) * 5/9 } } } var t = Temperature(celsius: 0) print(t.fahrenheit) // 32 t.fahrenheit = 212 print(t.celsius) // 100 ```

Shorthand get — Omitting get Keyword

For read-only properties, the `get { }` wrapper is optional: ```swift var area: Double { .pi * radius * radius } // same as { get { ... } } ```

newValue in set

Inside `set`, the incoming value is named `newValue` by default: ```swift set { celsius = (newValue - 32) * 5/9 } ``` Or name it explicitly: ```swift set(degF) { celsius = (degF - 32) * 5/9 } ```

Computed Properties Are Not Stored

Computed properties have no backing storage — every access runs the code: ```swift struct Stats { var scores: [Int] var average: Double { guard !scores.isEmpty else { return 0 } return Double(scores.reduce(0,+)) / Double(scores.count) } } ```

Static Computed Properties

```swift struct Config { static var appName: String { Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "App" } } print(Config.appName) ```

Computed Properties in Protocols

```swift protocol Greetable { var greeting: String { get } } struct Person: Greetable { var name: String var greeting: String { "Hello, \(name)!" } } ``` Protocols can require computed properties — just specify `{ get }` or `{ get set }`.

Computed vs Stored Performance

• Computed: recalculated on every access — avoid expensive computation in frequently-read properties • Use `lazy var` for expensive one-time computed values: ```swift lazy var thumbnail: UIImage = generateThumbnail() ```

Expression Body vs Block

Swift 5.9+ single-expression computed properties return implicitly: ```swift var fullName: String { "\(first) \(last)" } // equivalent to: var fullName: String { return "\(first) \(last)" } ```

Quick Check

What is the default name for the incoming value inside a property `set` block?

Recap

Key takeaways: • Computed properties run code on each access — no stored backing • Read-only: omit `get`/`set` or use shorthand `{ return ... }` • Read-write: provide `get` and `set(newValue)` blocks • Great for derived values like `area`, `fullName`, unit conversions • Use `lazy` for expensive once-computed values Next: mutating methods.

Frequently asked questions

Is the “Computed Properties in Structs” lesson free?

Yes — the full text of “Computed Properties in Structs” 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 “Computed Properties in Structs”?

Deriving values with get-only and get/set computed properties. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Computed Properties in Structs” 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. Memberwise Initializers and Custom Inits
  2. Stored Properties and Property Observers
  3. Computed Properties in Structs
  4. Mutating Methods and Copy-on-Write Semantics
← Back to Swift Academy