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

iOS Build a Simple iPhone App (iOS7) Refactoring into a Model Understanding @property

Clearly don't understand in what cases we use self.predictions and when _predictions

in method -(NSString *)randomPrediction{} we use

self.prediction

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!

May be it is because of that is not get/set method?

7 Answers

Stone Preston
Stone Preston
42,016 Points

using 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.

Thank 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
Stone Preston
42,016 Points

by 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

this

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
Stone Preston
42,016 Points

yes they should be provided you implemented your getters and setters correctly. can you post your getter and setter methods for your predictions property?

what would be the difference of implementation?

Stone Preston
Stone Preston
42,016 Points

it could be anything. in your getter/setters do you use _predictions?

I 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 }

I found that self used every time out of setter/getter. Is it correct?

Sorry 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;
}

I suppose I got the point. Thank you for your attention.