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 trialivan akulov
4,174 PointsClearly don't understand in what cases we use self.predictions and when _predictions
in method
-(NSString *)randomPrediction{}
we use
but in get method only
Why we cant use just
_prediction
, or always self.prediction
and how we can write ``` return [self.predictions objectAtIndex:randNumber];```in another way if it is possible?
Thank you so much!
7 Answers
Stone Preston
42,016 Pointsusing self.prediction calls the getter/setter methods behind the scenes. the reason you use _predictions in the getter/setter methods is because you want to modify the instance variable directly.
ivan akulov
4,174 PointsThank you for you fast answer! and sorry, as it is still not really clear what does it mean "behind the scenes". But I tried to write
return [_predictions objectAtIndex:randNumber];
it doesn't work fine. It makes predictionLabel.text = @""
empty? No guess
Stone Preston
42,016 Pointsby behind the scenes I mean using self.predictions to get a variable really translates to [self predictions] and using self.predictions to set a variable translates to [self setPredictions]. so it calls the setter and getter methods whereas using _predictions lets you manipulate and get the variable directly, without using setter/getter methods
ivan akulov
4,174 Pointsthis
return [self.predictions objectAtIndex:randNumber];
and this
return [_predictions objectAtIndex:randNumber];
Supposed to be equal? If yes I got not fine code to work with 2nd variant
Stone Preston
42,016 Pointsyes they should be provided you implemented your getters and setters correctly. can you post your getter and setter methods for your predictions property?
ivan akulov
4,174 Pointswhat would be the difference of implementation?
Stone Preston
42,016 Pointsit could be anything. in your getter/setters do you use _predictions?
ivan akulov
4,174 PointsI did the same as in video. But I made a mistake that helps me to understand what I don't understand - self. As I used to think _predictions and self.predictions it is the same, but it is not. here is method
-(NSArray *)prediction;{
//here I use _prediction as in video example
}
ivan akulov
4,174 PointsI found that self used every time out of setter/getter. Is it correct?
ivan akulov
4,174 PointsSorry didn't see you asking for code
.h
@interface p1us_CrystalBall : NSObject{
NSArray *_predictions;
}
@property (strong, nonatomic, readonly) NSArray *predictions;
-(NSArray *)predictions{
.m
if(_predictions == nil){
_predictions = [[NSArray alloc] initWithObjects:
@"It is certan",
@"It is decidedly so",
@"All signs say YES",
@"The stars are not alinged",
@"My reply is no",
@"It is doubtful",
@"Better not tell you",
@"Concentrate and ask again",
@"Unable to answer now", nil];
}
return _predictions;
}
ivan akulov
4,174 PointsI suppose I got the point. Thank you for your attention.
ivan akulov
4,174 Pointsivan akulov
4,174 PointsMay be it is because of that is not get/set method?