Swift’s self vs Self vs Self.self: What’s the Difference?
Updated: Feb 25, 2025
If you've ever found yourself confused by Swift's self, Self, and Self.self, you're not alone. These keywords look similar but serve different purposes depending on context. This guide will walk you through their meanings with practical examples to make them easy to understand and apply.

Quick Overview
Expression | Meaning | Common Use Case |
self | Refers to the current instance | Accessing instance properties or methods. |
Self | Refers to the current type | Used in protocols, class methods, and fluent interfaces. |
Self.self | Refers to the metatype of Self | Accessing the type object for reflection or passing as a value. |
✅ 1. self (lowercase)
📖 What is it?
self refers to the current instance of a type. Think of it like this in languages like Java or JavaScript.
🔑 When to use self:
To clarify property access when there are naming conflicts.
To capture the instance within closures.
💻 Example:
struct Person {
var name: String
func printName() {
print(self.name) // Refers to the current instance's
// property.
}
mutating func updateName(to name: String) {
self.name = name // Disambiguates between the property and
// parameter.
}
}
var john = Person(name: "John")
john.printName() // Prints: John
john.updateName(to: "Johnny")
john.printName() // Prints: Johnny🧠 Pro Tip:
In many cases, you can omit self, but Swift requires it in closures (to avoid capture issues) or when disambiguating names.
✅ 2. Self (uppercase)
📖 What is it?
Self refers to the type itself, not an instance. It's commonly used in protocols, type methods, and to return the conforming type in methods.
🔑 When to use Self:
When defining methods in protocols that should return the conforming type.
In classes and structs to refer to the current type in a type-safe way.
💻 Example in Protocols:
protocol Cloneable {
func clone() -> Self
}
struct Document: Cloneable {
func clone() -> Self {
return self // 'Self' refers to 'Document' here.
}
}
let doc = Document()
let clonedDoc = doc.clone() // Type-safe cloning💻 Example in Class Methods:
class Animal {
class func create() -> Self { // 'Self' ensures subclasses
// return their own type.
return Self.init()
}
required init() {}
}
class Dog: Animal {}
let dog = Dog.create() // 'dog' is of type Dog, not Animal.🧠 Why use Self instead of the type name?
Self makes code more generic and flexible, especially in protocols or base classes. It ensures that returning methods adapt to the conforming or subclass type.
✅ 3. Self.self
📖 What is it?
Self.self refers to the type object itself, often used for reflection or when you need to pass a type as a value.
🔑 When to use Self.self:
To obtain the metatype (Type) of a given type.
When you need to pass the type to a function or initializer.
In protocols to describe the conforming type.
💻 Example:
struct Example {}
let type: Example.Type = Example.self // Getting the metatype.
print(type) // Prints: Example🔍 Where is Self.self here? In this example, you’re using Example.self, not Self.self. When you reference a type directly (like Example), you use the .self suffix to get its metatype. If you were inside a context where Self refers to a type (e.g., inside a protocol extension or a class method), then Self.self would be the equivalent reference:
protocol Describable {
static func describeType()
}
extension Describable {
static func describeType() {
print(Self.self) // Refers to the type conforming to
// Describable.
}
}
struct Book: Describable {}
Book.describeType() // Prints: Book🧠 Why use Self.self?
When you need to handle types as first-class values, like passing them into factory methods or using Swift’s reflection APIs, Self.self comes into play.
🔔 Final Thoughts
Understanding the differences between self, Self, and Self.self is crucial for writing clean, type-safe Swift code.
Use self for instance context.
Use Self for type context, especially in protocols or class methods.
Use Self.self when you need the type itself as a value.
Mastering these concepts will not only help you write better Swift code but also deepen your understanding of the language's type system.




Comments