Difference between using self and not using self in Swift init

I noticed that property initialization in the Swift initializer works using both:

self.property = 1

and

property = 1

Is there a difference between the two? If not, is there an agreement that supports one over the other?

+4
source share
1 answer

In the first, you make it explicit that this is a class / struct property, while in the second, it is implicit. There is one big difference: if there is a local variable with the same name (for example, the parameter passed to init), it will take priority and hide the class / struct property.

, self. , , , , , .

+10

All Articles