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
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.
📋 Quick Reference
No blanks this time — just a brief and an outline. Build it, run it, and check your output against the example in the comments.
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.