Skip to content
Prepare to Grow

Swift Fundamentals

Properties, Methods, and Initializers

Build well-formed Swift types with stored state, derived values, behavior, and safe initialization.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Distinguish stored and computed properties.
  • Write instance and type methods.
  • Initialize every stored property safely.
  • Use self, mutating, and access control intentionally.
01

Learning stage

Concept Explanation

Properties store or compute information that belongs to a type. Stored properties keep values, while computed properties derive a value through a getter and an optional setter without adding separate storage.

Methods define behavior. A structure method that changes stored properties must be marked mutating. Initializers prepare a new instance and must ensure that every non-optional stored property has a value before initialization completes.

  • Stored properties preserve instance data.
  • Computed properties derive values on demand.
  • Instance methods work with one instance; type methods belong to the type itself.
  • init establishes a complete, valid initial state.
02

Learning stage

Syntax & Examples

This value type owns stored progress, derives a percentage, and protects mutation behind a method.

CourseProgress.swiftswift
struct CourseProgress {
  let totalLessons: Int
  private(set) var completedLessons: Int

  var percentage: Double {
      Double(completedLessons) / Double(totalLessons) * 100
  }

  init(totalLessons: Int) {
      self.totalLessons = max(totalLessons, 1)
      completedLessons = 0
  }

  mutating func completeNextLesson() {
      guard completedLessons < totalLessons else { return }
      completedLessons += 1
  }
}

var progress = CourseProgress(totalLessons: 14)
progress.completeNextLesson()
print("Progress: \(progress.percentage)%")
Expected output is approximately “Progress: 7.142857%”.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Keep model invariants inside initializers instead of repairing invalid instances later.
  • Expose derived display or domain values with computed properties.
  • Use methods to give mutations meaningful names and controlled rules.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Leaving stored properties without a value when initialization completes.
  • Storing a value that can be derived reliably from other properties.
  • Changing a structure inside a method without marking the method mutating.
  • Exposing unrestricted setters when changes must follow domain rules.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Create a measured value

Define a Rectangle with width and height stored properties, a computed area, and an initializer that prevents negative dimensions.

Hint: Decide whether to clamp invalid values or make the initializer failable.

Activity 2

Control mutation

Create a StudyStreak type whose count can be read publicly but changed only through increment and reset methods.

Hint: private(set) can restrict direct writes.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Does a read-only computed property store a separate value?

Reveal answer

No. It calculates and returns a value when accessed.

QUESTION 2

What must be true when Swift initialization finishes?

Reveal answer

Every stored property must have a valid initialized value, unless it already has a default or is an optional initialized to nil.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

How do stored and computed properties differ?

Short answer: Stored properties keep data; computed properties calculate a value through accessors.

Explore the detailed answer

A stored property contributes to an instance's stored state. A computed property defines a getter and optionally a setter, deriving its result from other information. Computed properties are useful when storing the same information separately would create synchronization problems.

Possible follow-ups

  • • Why does a struct method need mutating?
  • • What does an initializer guarantee?
08

Learning stage

Key Takeaways

A useful type protects a valid state and gives its behavior meaningful names. Initialization establishes the state; properties expose information; methods coordinate change.

  • Stored and computed properties serve different roles.
  • Initializers must produce complete instances.
  • Struct mutations require mutating methods.
  • Access control can protect domain rules.