Distinction in Swift between uppercase "Self" and lowercase "self"

While playing in a Swift playground I noticed that Self, with capital "S", is available along with the lowercase self. Is there any difference between them? If so, what are usages for these two, especially for Self?


Solution 1:

Self refers to the type of the current "thing" inside of a protocol (whatever is conforming to the protocol). For an example of its use, see Protocol func returning Self.

The official docs I've found for Self is in Protocol Associated Type Declaration in The Swift Programming Language. It surprisingly is not documented in the sections on protocols or on nested types:

However, now there is a paragraph about Self Type including a code example in the official Swift Programming Language's chapter about Types

Solution 2:

Self can also be used in classes, and is useful. Here is an article about it.

Here is an example. You have a class called MyClass. MyClass have methods returning instances of it. Now you add a subclass of MyClass called MySubclass. You want these methods to return instances of MySubclass instead of MyClass. The following code shows how to do it. Note that the methods can be either instance methods or class methods.

class MyClass: CustomStringConvertible {

    let text: String

    // Use required to ensure subclasses also have this init method
    required init(text: String) {
        self.text = text
    }

    class func create() -> Self {
        return self.init(text: "Created")
    }

    func modify() -> Self {
        return type(of: self).init(text: "modifid: " + text)
    }

    var description: String {
        return text
    }

}

class MySubclass: MyClass {
    required init(text: String) {
        super.init(text: "MySubclass " + text)
    }
}

let myClass = MyClass.create()
let myClassModified = myClass.modify()

let mySubclass = MySubclass.create()
let mySubclassModified = mySubclass.modify()

print(myClass)
print(myClassModified)
print(mySubclass)
print(mySubclassModified)

The following line printed out:

// Created
// modifid: Created
// MySubclass Created
// MySubclass modifid: MySubclass Created

Solution 3:

in protocol & Extension declaration use Self else self

extension protocolName where Self: UIView 
{
  func someFunction()
  {
    self.layer.shadowColor = UIColor.red.cgColor
  }
}

Solution 4:

I think this question could use a simpler answer, more focussed on the difference between Self and self, and perhaps aimed at people newer to Swift.

self - explicit reference to the current type or instance of the type in which it occurs.

class MyClass {
  func showClass() {
       print("\(self)")
    }
  }
let someClass = MyClass()
someClass.showClass()
// prints "MyClass"

Self - Used specifically in protocol and extension declarations to refer to the eventual type that will conform to the protocol.

protocol MyProtocol {
   static func returnSelf() -> Self
}

class MyClass: MyProtocol {
   // define class
}

MyClass.returnSelf() 
// returns MyClass

The difference is that self is used in types and instances of types to refer to the type that it's in; and Self is used in protocols and extensions where the actual type is not yet known.

In even simpler terms, self is used within an existing type; Self is used to refer to a type that Self is not yet in.

Read more here:

  • Self - https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_543
  • self - https://docs.swift.org/swift-book/ReferenceManual/Expressions.html
  • Both - https://medium.com/the-traveled-ios-developers-guide/swift-keywords-v-3-0-1-f59783bf26c

Solution 5:

I understand Self as a type name(class name for example) and self as an instance of a class/struct , for example:

struct Person {
     static var documentNumner = "9484930"
     var name: String
     var computedFullName: String {
        return ("\(self.name) with document number: \(Self.documentNumner)")

    }
}

You can't use self with a static property but you can use Self