Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

readonly discussion

After following along with the readonly discussion video, my crystal ball app is no longer displaying the information from the NSArray after I click on the prediction button. I am not sure why this is. Does anybody have any ideas that might help? As far as I can tell the NSArray still contains all of the string items, but they will not display after the button is pressed.

Stone Preston
Stone Preston
42,016 Points

post your code for the button press please.

This is for the view controller.m file.

import "FFViewController.h"

import "FFCrystalBall.h"

@interface FFViewController ()

@end

@implementation FFViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.crystalBall = [[FFCrystalBall alloc] init]; }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }

  • (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; } @end

2 Answers

This is for the view controller.m file.

import "FFViewController.h"

import "FFCrystalBall.h"

@interface FFViewController ()

@end

@implementation FFViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.crystalBall = [[FFCrystalBall alloc] init]; }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }

  • (IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; } @end

Stone Preston
Stone Preston
42,016 Points

post the code for the cystal ball file as well.

This is for the crystal ball .m file.

import "FFCrystalBall.h"

@implementation FFCrystalBall

  • (NSArray *) predictons { if (_predictions == nil) { _predictions = [[NSArray alloc] initWithObjects:@"It is Certain", @"It is decidedly so", @"All signs say YES", @"The stars are not aligned", @"My reply is no", @"It is doubtful", @"Better not tell you now", @"Concentrate and ask again", @"Unable to answer now", nil]; } return _predictions; }

  • (NSString*) randomPrediction { int random = arc4random_uniform(self.predictions.count); return [self.predictions objectAtIndex:random]; }

@end

hey what i found wrong was the spelling of predictions array. you wrote: (NSArray *) predictons where as it should be predictions. I hope now it should work.

Thank you so much! That was exactly the problem. It is working as expected now, thank you.