I read many publications on "self" in Swift and I am starting to get a gist of it, but there is still one thing that is unclear to me.
class Car {
// 1
let make: String
// 2
private(set) var color: String
init() {
make = "Ford"
color = "Black"
}
required init(make: String, color: String) {
self.make = make
self.color = color
}
// 3
func paint(color: String) {
self.color = color
}
}
let car = Car(make: "Tesla", color: "Red")
car.paint("Blue")
I am trying to prove my point with help from the example above.
Several publications that I read indicate that self
is used to distinguish 'color' from init() from the 'color' in the parameter from the func paint(color: String)
.
So when 'self color' is set in the func paint(color: String)
, which 'color' is it referring to? 'color' from the init()
or color from the parameter of func paint(color: String)
?