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 trial
Connor Palindat
1,735 PointsQuestion about "self"
Hi! Sorry as this was probably already mentioned in one of the other videos, but when changing the label text to yes through: self.predictionLabel.text = ...
What is the point of "self"?
1 Answer
Stone Preston
42,016 Pointsself represents an instance of the class calling the method in instance methods, and the class itself in class methods.
here is your code:
self.predictionLabel.text = ...
self in this case represents an instance of your viewController class
Its kind of hard to understand at first, but when implementing a class such as a view controller, sometimes you want to call some instance methods or reference some instance variables inside the class itself. for example. if we had a class named cat
@implementation Cat
- (void)meow {
NSLog(@"meow");
}
you can see that Cat has an instance method called meow. lets add another method called eat that calls that meow method inside it. But since meow is an instance method, which are called on instances or objects, how do we call that method when we dont have an instance to call it on since we are currently implementing the class itself? The solution is to use the keyword self which represents an instance of the current class.
- (void)eat {
NSLog(@"eating");
[self meow];
}
using [self meow] basically says call the meow instance method on the current instance of Cat. in other languages instead of the keyword being self the keyword is this, which is a bit easier to understand. in java if we wanted to call the meow method we would use this.meow() where this refers to "this instance of cat"
so if we created a cat object and called the eat method
Cat *someCat = [[Cat alloc] init];
[someCat eat];
the compiler would go to the eat method of the cat class and execute the code there
- (void)eat {
NSLog(@"eating");
[self meow]; //self here refers to the instance of cat that called the eat method, which is someCat
}
since someCat was the instance that called the method, self refers to the someCat object
when working inside class methods, using self just refers to the class itself. So you can use self to call class level methods etc. this way of using self is a lot easier to understand since its more simple. if you need to call a class method or refer to the class itself inside a class implementation, you can just use self ie [self someClassMethod] which is the same as [Classname someClassmethod]
cameryn smith
2,828 Pointscameryn smith
2,828 PointsWow, I actually got that thx.