Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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?

like image 324
Eray Diler Avatar asked Jan 09 '15 15:01

Eray Diler


People also ask

What is difference between self and self in Swift?

“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.

What's the difference between self and self?

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.

What is the meaning of self in Swift?

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.

Why do we write self in Swift?

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.


2 Answers

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

like image 181
Rob Napier Avatar answered Sep 21 '22 05:09

Rob Napier


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 
like image 44
ukim Avatar answered Sep 18 '22 05:09

ukim