What's the Long Version of Statements That Use Self?

Hi. Self is confusing, still not sure what's it for (even after studing Swift for more than 7months to more than a year already). What's the long version if things do not use self?


init(name: String) {

self.name = name

}


Thank you in advance.

God bless, Proverbs 31

Accepted Reply

self.name = name this would be the MyClass's var name: String property?

What about the name on the right side = name is that also MyClass' var name: String property?


self.name is the class property

name is the parameter passed to init()


To see the difference, you could write

    init(withName: String) {
        self.name = withName
    }

and call as

let obj1 = MyClass(withName: "A")



So that's like var name = var name (itself = itself)?

Not at all, see above.


class MyClass {
     var name: String = "Has the string value"

Is self.name = name 's value defaulted to the void/null value like an optional?


What do you mean ? not defaulted to nil here.

name is initialized with a default value, but that can be changed when creating or modyfing the instance.

Once again, if that is to hard to understand, whange int(withName: String) to better understand.



What happens if you don't put the initialization

Compiler will detect an error, unless you give an initial value when declaring the var or declare the var as optional (it gets nil value)


Any real world anology on the concept of initialization and self?

This is always tricky and often misleading to try to illustrate by real world examples and does not replace the need for a real understanding of what happens in computer world.


However, I would say:

Class is somehow the mold. It is not an object.

With the mold, you define instructions for operator, like :

- object will be of some material : clay, plaster, … ; no default value, so material woul be defined as optional value

- object will have a color (that's a property) ; by default the color will be brown

When you instantiate, you create an object instance from this mold. Now you have a physical object in hand.

But to create, you need to use some material and taint it: that's the role of init

int(material: "clay", color: blue)

this tells the operator what he has to do: use that material and taint it blue. Then of course mold it to create the object instance.

self in the instruction manual is "the object you are creating"


So the sequence is:

let obj1 = MyClass(material: "Clay", color: "Blue")

-> Operator knows he has to use clay and blue tint


The instruction manual will say

init(material: SomeMaterial, color: SomeColor)

-> To create an object from the mold, use the material and color defined in the order

-> self is referring to this real object you are creating


Hope that helps.

Replies

There's no Long Version of `self`. `self` is `self`. Seems you have been going through a wrong path.


Have you learnt the concept of `class` and `instance`? It's the very basic part of Object Oriented Programming language like Swift.


Instance methods or the bodies of initializers are called on a specific instance.

Instance properties needs to be accessed on a specific instance.


You need to specify the instance. `self` specifies the very instance, tageted for the initializer or the enclosing method.


class MyClass {
    var name: String
    
    func method1() {
        print(#function, self.name)
    }
    
    init(name: String) {
        self.name = name
    }
}
let obj1 = MyClass(name: "A")
let obj2 = MyClass(name: "B")

obj1.method1() //->method1() A
obj2.method1() //->method1() B

In the above example `obj1` and `obj2` hold the different two instances of `MyClass` repectively.


When you specify `obj1` when calling `method1`, `self` represents the same instance held in `obj1`. When using `obj2` as well.


In some cases, you can omit `self.`, so there may be some short version of `self.`, but no Long Version.

Thanks. In self.name = name the left side of this equation (or is it a declaration or is it a statement?) self.name would be the MyClass's

var name: String property?


What about name on the right side = name, is that also MyClass' var name: String property?


So that's like var name = var name (itself = itself)?

How's that initializing name? When name has no value? Unlike if you hardcoded the value like:


class MyClass {

var name: String = "Has the string value"


Is self.name = name 's value defaulted to the void/null value like an optional?


What happens if you don't put the initialization (is this a kind of function by the way?). Any real world anology on the concept of initialization and self?


Thanks.

self.name = name this would be the MyClass's var name: String property?

What about the name on the right side = name is that also MyClass' var name: String property?


self.name is the class property

name is the parameter passed to init()


To see the difference, you could write

    init(withName: String) {
        self.name = withName
    }

and call as

let obj1 = MyClass(withName: "A")



So that's like var name = var name (itself = itself)?

Not at all, see above.


class MyClass {
     var name: String = "Has the string value"

Is self.name = name 's value defaulted to the void/null value like an optional?


What do you mean ? not defaulted to nil here.

name is initialized with a default value, but that can be changed when creating or modyfing the instance.

Once again, if that is to hard to understand, whange int(withName: String) to better understand.



What happens if you don't put the initialization

Compiler will detect an error, unless you give an initial value when declaring the var or declare the var as optional (it gets nil value)


Any real world anology on the concept of initialization and self?

This is always tricky and often misleading to try to illustrate by real world examples and does not replace the need for a real understanding of what happens in computer world.


However, I would say:

Class is somehow the mold. It is not an object.

With the mold, you define instructions for operator, like :

- object will be of some material : clay, plaster, … ; no default value, so material woul be defined as optional value

- object will have a color (that's a property) ; by default the color will be brown

When you instantiate, you create an object instance from this mold. Now you have a physical object in hand.

But to create, you need to use some material and taint it: that's the role of init

int(material: "clay", color: blue)

this tells the operator what he has to do: use that material and taint it blue. Then of course mold it to create the object instance.

self in the instruction manual is "the object you are creating"


So the sequence is:

let obj1 = MyClass(material: "Clay", color: "Blue")

-> Operator knows he has to use clay and blue tint


The instruction manual will say

init(material: SomeMaterial, color: SomeColor)

-> To create an object from the mold, use the material and color defined in the order

-> self is referring to this real object you are creating


Hope that helps.

In self.name = name the left side of this equation (or is it a declaration or is it a statement?) self.name would be the MyClass's

var name: String property?

You should better call it an assignment or an assignment statement. It's not an equation nor a declaration.


And for the latter, yes. `self.name` represents the instance property of `MyClass` where currently instantiated.


What about name on the right side = name, is that also MyClass' var name: String property?


No, it's a formal argument passed to the initializer. You declare the initializer as `init(name: String)`, the right hand side `name` represents this `name`.


So that's like var name = var name (itself = itself)?

How's that initializing name? When name has no value? Unlike if you hardcoded the value like:

Is self.name = name 's value defaulted to the void/null value like an optional?


As noted above, the line is completely different than itself = itself.


Any real world anology on the concept of initialization and self?


You may need to know the very basic part of Object Oriented languages.

A class is just a blueprint and an instance is sort of a product based on the blueprint.

Initializer defines the initial setup of the product and `self` represents the currently produced product.