What is the difference between self and Self in iOS?

By | January 16, 2024

In iOS development with Swift, self and Self have distinct meanings and use cases.

  1. self:
  • In Swift, self is used to refer to the current instance of the type. It is commonly used within instance methods and closures to access properties and methods of the current instance.
  • Example:
class MyClass {
    var value: Int = 10

    func printValue() {

In the printValue method, self is used to explicitly refer to the value property of the current instance.

2. Self:

  • With a capital “S,” Self refers to the type itself. It is often used in the context of protocol-oriented programming, where it represents the conforming type.
  • Example:
protocol MyProtocol {
    static func createInstance() -> Self

class MyClass: MyProtocol {
    required init() {}

    static func createInstance() -> Self {
        return self.init()

In this example, Self is used as the return type of the createInstance method. It indicates that the method will return an instance of the same type that adopts the protocol.

In summary, self refers to the current instance of a type, while Self (with a capital “S”) refers to the type itself, often used in the context of protocols and associated types. It’s important to use them in the appropriate context to avoid confusion and ensure correct behavior in your Swift code.

Leave a Reply

Your email address will not be published. Required fields are marked *