100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Properties and Methods in Swift

Properties store or compute values on a type, while methods define the functions and behavior attached to it.

Structures & ClassesIntermediate10 min readJul 8, 2026
Analogies

Introduction

Properties and methods are how structs, classes, and enums expose data and behavior. Properties come in two flavors: stored properties that hold an actual value in memory, and computed properties that calculate a value on demand via a getter and optionally a setter. Methods are functions associated with a particular type; instance methods operate on a specific instance, while type methods, declared with static func, operate on the type itself.

🏏

Cricket analogy: A batsman's career strike rate is stored after each match like a stored property, while his current-series average is a computed property recalculated from stored innings data every time you check it; team methods like declareInnings() act on the whole XI, like a static function.

Syntax

swift
struct Rectangle {
    var width: Double
    var height: Double

    var area: Double {          // computed property
        return width * height
    }

    mutating func scale(by factor: Double) {
        width *= factor
        height *= factor
    }

    static func unitSquare() -> Rectangle {
        Rectangle(width: 1, height: 1)
    }
}

Explanation

width and height are stored properties, holding actual values. area is a computed property — it has no storage of its own and instead recalculates its value from width and height every time it's accessed. scale(by:) is an instance method that mutates the struct's own properties; because structs are value types, any instance method that changes self must be marked mutating. unitSquare() is a type method, callable on the type itself (Rectangle.unitSquare()) rather than on an instance. Property observers willSet and didSet can be attached to stored properties to run code just before or after a value changes.

🏏

Cricket analogy: A player's stored runs total sits in the scorebook like width and height, but strike rate is a computed property recalculated from runs and balls faced every time it's displayed; changing your batting stance mid-innings is like a mutating method, and Team.selectSquad() is a type method run once before the match.

Example

swift
struct Rectangle {
    var width: Double { didSet { print("width changed to \(width)") } }
    var height: Double
    var area: Double { width * height }

    mutating func scale(by factor: Double) {
        width *= factor
        height *= factor
    }
}

var r = Rectangle(width: 3, height: 4)
print("area = \(r.area)")
r.scale(by: 2)
print("area = \(r.area)")

Output

swift
area = 12.0
width changed to 6.0
area = 24.0

Key Takeaways

  • Stored properties hold actual values; computed properties calculate a value on access.
  • Property observers willSet and didSet run code around a stored property's value changing.
  • Instance methods operate on a specific instance; type methods use static func and operate on the type.
  • Struct instance methods that mutate self must be marked mutating.
  • Computed properties can have a getter only, or both a getter and a setter.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#PropertiesAndMethodsInSwift#Properties#Methods#Syntax#Explanation#Functions#StudyNotes#SkillVeris