This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021


You’ve probably used the keyword self a lot when writing code, but have you ever wondered what self is? Let’s take a look today:

  • What are self, self, and self.type?
  • Under what circumstances are they used?

self

This is one that you use a lot, self is usually used when you need to refer to an object in your current scope. So, for example, if self is used in an instance method of Rocket, in this case self will be an instance of the Rocket. This is easy to understand

struct Rocket {
    func launch(a) {
        print("Launch in 10 seconds\ [self)")}}let rocket = Rocket()
rocket.launch() // Fire a Rocket in 10 seconds
Copy the code

But what if you want to use self in a class method or a static method? In this case, self cannot be used as a reference to the instance, because there is no instance and self has the value of the current type. This is because static and class methods exist on the types themselves, not on the instances.

class Dog {
    class func bark(a){
        print("\ [self)Auf!")}}Dog.bark() //Dog woof woof!


struct Cat {
    static func meow(a) {
        print("\ [self)Meow meow meow!")}}Cat.meow() // Cat meow meow meow!
Copy the code

Yuan type

There’s another caveat. All values should have a type, including self. As mentioned above, static and class methods exist on types, so in this case self has a Type: self.type. For example, dog. Type stores all Dog Type values.

Types that contain other types are called metatypes.

In short, the meta-type dog. Type holds not only the value of the Dog Type, but also the value of all of its subclasses. Take the following example, where Labrador is a subclass of Dog.

class Dog {
    class func bark(a){
        print("\ [self)Auf!")}}class Labrador: Dog {}Labrador.bark()    //Labrador woof woof!
Copy the code

If you want to treat type itself as an attribute or pass it into a function, you can also use type itself as a value. In this case, you can use: type.self.

let dogType: Dog.Type = Labrador.self

func saySomething(dog: Dog.Type) {
    print("\(dog)Auf!")
}

saySomething(dog: dogType) // Labrador woof woof!
Copy the code

Self

And then finally, Self starting with capital S. This is useful when creating factory methods or when returning concrete types from protocol methods:

struct Rocket {
    func launch(a) {
        print("Launch in 10 seconds\ [self)")}}extension Rocket {
    static func makeRocket(a) -> Self {
        return Rocket()}}protocol Factory {
    func make(a) -> Self
}

extension Rocket: Factory {
    func make(a) -> Rocket {
        return Rocket()}}Copy the code