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
?
“Self” as a Type in Swift This “Self” has an uppercase “S”, which is how you can tell it apart from the lowercase self. In Swift, Self refers to a type – usually the current type in the current context. Just as lowercase self can mean any current object, uppercase Self can mean just about any current type.
When used with a capital S, Self refers to the type that conform to the protocol, e.g. String or Int . When used with a lowercase S, self refers to the value inside that type, e.g. “hello” or 556.
In Swift self is a special property of an instance that holds the instance itself. Most of the times self appears in an initializer or method of a class, structure or enumeration. The motto favor clarity over brevity is a valuable strategy to follow.
It's used to access class, structure and enumeration instance within methods. When self is accessed in a type method like static func or class func , it refers to the actual type rather than an instance. Swift allows omitting self when you want to access instances properties.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With