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 Readonly Attribute

Getter method not called when @CrystalBall created

Sorry for the poor explanation - I'm new at this.

When I initiate a CrystalBall class, it should be filling the 'predictions' variable with an array of string predictions. Even though it creates the array,it doesn't call the get method. Leaving the predictions variable empty.

Could someone give some hint or advice on what simple step I'm overlooking.

Thanks!

As a work around I did the following -

- (void)viewDidLoad {

   [super viewDidLoad];

    self.crystalBall = [[JBCrystalBall alloc] init];

    [self.crystalBall predictions];   // Calling the getter method here.... but shouldn't is do this when it's created??
}

4 Answers

ok when we type this line - [self.predictions objectAtIndex:random]

then, the predictions function is called ( which is a getter for the NSArray predictions) there it will check if the predictions array is nil .(it will be nil, if the crystal ball app has just been launched and is being used for the first time) , if the predictions array is nil then first, it will be initialized with all the string values. and then it will be returned to the randomPredictions function where a random object will be passed to the THviewController class .

I hope am able to clarify your doubt ....

Hi Jeff,

What i could understand from the question you have put... The get method is not required since we have created the predictions array as a property , so we can access the objects in the predictions array through dot notation... so no get or set methods required.

In the CrystalBall class.... the randomPrediction method calls a random object from the predictions array using the following line: return [self.predictions objectAtIndex:random]; self.predictions is a way of using the get method.

I hope it has helped a little in clarifying your doubt...

Thanks for the response! Let me just say this in some other words to make sure I get it.

When I type - [self.predictions objectAtIndex:random]

The first part, self.predictions, is filling the predictions property with the values of the array? Before this point, the predictions property was nil, right?

Thanks for the response! Let me just say this in some other words to make sure I get it.

When I type - [self.predictions objectAtIndex:random]

The first part, self.predictions, is filling the predictions property with the values of the array?
Before this point, the predictions property was nil, right?

Thanks Rashu! Keep up the great work.