Methods & Initializers
Functions that live inside your types become methods . In this lesson you'll write instance and type methods, use mutating on structs, master self , and set up new values with designated and convenience init ializers.
Learn Methods & Initializers in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
- • Write instance methods that read and change a type's properties
- • Mark struct methods mutating when they modify the instance
- • Use self to refer to the current instance
- • Define type methods with static (and class func )
- • Initialize values with init
- • Tell designated from convenience initializers
1️⃣ Instance Methods
An instance method is a function written inside a type. It can read the instance's properties directly. Because a struct is a value type , any method that changes a property must be marked mutating — Swift's way of making mutation explicit.
2️⃣ The self Keyword
self refers to the current instance. You only need it when a local name (often an init parameter) would otherwise hide a property of the same name. Elsewhere it's optional.
3️⃣ Type Methods
A type method belongs to the type itself, not an instance. Mark it static and call it on the type, like Temperature.fromFahrenheit(212) . These are perfect for factory helpers and shared constants.
4️⃣ Initializers: Designated vs Convenience
An initializer prepares a new instance by giving every stored property a value. In a class a designated init fully sets up the object, while a convenience init is a helper that must delegate to another init in the same class with self.init(...) .
Your turn. Fill in the blank so the deposit method can change the balance.
Pro Tips
- 💡 Only add mutating when you must. A method that just reads properties doesn't need it.
- 💡 Use type methods for factories. A static func make... reads better than scattering setup code.
- 💡 Keep designated inits simple. Push convenience defaults into convenience inits that call self.init .
- 💡 Write self. only when needed. Reach for it when a parameter shadows a property.
Common Errors (and the fix)
- "Cannot assign to property: 'self' is immutable" — a struct method changed a property without mutating . Add the keyword.
- "Return from initializer without initializing all stored properties" — a designated init left a property unset. Assign every stored property.
- "Cannot call value of non-function type" — you called a type method on an instance, or vice versa. Call type methods on the type name.
- "Self.init isn't called on all paths in delegating initializer" — a convenience init must always call self.init(...) .
📋 Quick Reference
Task
Code
Note
Instance method
func go() { … }
on an instance
Mutating
mutating func add() { … }
struct changes self
Type method
static func make() { … }
on the type
Designated init
init(x: Int) { … }
sets all props
Convenience init
convenience init() { self.init(...) }
delegates
Frequently Asked Questions
Mini-Challenge: Thermostat
No blanks this time — just a brief and an outline. Build it, run it, and check your output against the example in the comments.
🎉 Lesson Complete!
- ✅ Methods are functions defined inside a type
- ✅ Struct methods that change properties need mutating
- ✅ self refers to the current instance; required when names clash
- ✅ Type methods use static and are called on the type
- ✅ Convenience inits delegate to another init with self.init
- ✅ Next lesson: Closures in Depth
Practice quiz
What is a method in Swift?
- A stored property
- A global constant
- A function that belongs to a type
- A protocol
Answer: A function that belongs to a type. A method is simply a function defined inside a type such as a struct, class, or enum.
What does the 'self' keyword refer to inside an instance method?
- The current instance the method is called on
- The type itself
- The superclass
- A new copy of the instance
Answer: The current instance the method is called on. Inside an instance method, 'self' refers to the specific instance the method was called on.
What keyword marks a method that belongs to the TYPE rather than an instance?
- instance
- class only
- shared
- static
Answer: static. The 'static' keyword (or 'class' in a class, to allow overriding) defines a type method.
What is the purpose of an initializer (init)?
- To deinitialize an object
- To set up a new instance's stored properties
- To copy an instance
- To compare two instances
Answer: To set up a new instance's stored properties. An initializer prepares a new instance by giving every stored property an initial value.
Why must an instance method on a STRUCT be marked 'mutating' to change a property?
- Structs are value types, so methods can't change them by default
- Structs are reference types
- Mutating is required on every struct method
- It improves performance
Answer: Structs are value types, so methods can't change them by default. Structs are value types; a method must be marked 'mutating' to modify the instance's properties.
What is a convenience initializer in a class?
- An init that fully initializes all properties directly
- An init with no parameters
- A secondary init that must call another init in the same class
- An init that can't be overridden
Answer: A secondary init that must call another init in the same class. A convenience initializer must call another initializer (designated or convenience) in the same class.
What must a designated initializer of a class do?
- Call a convenience init
- Fully initialize all stored properties introduced by its class
- Always have parameters
- Return self
Answer: Fully initialize all stored properties introduced by its class. A designated initializer is responsible for fully initializing all stored properties of its class.
When is 'self' required (not optional) inside an initializer?
- Never
- Only in classes
- Only for computed properties
- When a parameter name shadows a property name
Answer: When a parameter name shadows a property name. When a parameter has the same name as a property, you write self.name = name to disambiguate.
How do you call a type method named 'create' on a type named Robot?
- create()
- Robot.create()
- self.create()
- Robot().create()
Answer: Robot.create(). Type methods are called on the type itself: Robot.create().
What keyword (instead of 'static') lets a class type method be overridden by subclasses?
- override
- virtual
- class
- open
Answer: class. Using 'class func' instead of 'static func' allows subclasses to override the type method.
Continue this course
- Previous: Properties: Stored, Computed & Observers
- Next: Closures in Depth