Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial3 Answers
Kyle Johnson
33,528 PointsHere's a link to the Swift documentation on Methods and the self property.
meitav asulin
1,123 Pointsactually, I already read about that and I couldn't understand that.I s it like to say what stored in the value = value?
Chris Sehnert
30,857 PointsEach new instance of an object you create (ie "instantiate) through the init-method... needs to have it's own versions of the stored properties that come along with the class they belong to..... for example if you create a class of Car that has a stored property such as ... var color: String .....than each new instance of the Car class must have a particular color property of type String...... so that you might reference.... myNewCar.color ...and get back a String value.... Therefore "self" with in a class init method refers to the new instance that will be created at instantiation....
class Car {
var color: String // stored property "color" of type String must be included with each new Car instance....
init(color: String) {
self.color = color // each new version of Car (self) will be assigned the color provided at instantiation.....
}
}
let myNewCar = Car(color: "Red") //example of Car instantiation......(instantiation == creating a new "instance" of the class)
if (myNewCar.color == "Red") { print("I understand.......'self' refers to each new instance!") }