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

Crystal Ball extra credit

I'm having a a great deal of trouble figuring out how to create the NSArray [UIColor] object to add random color to the prediction label text This is what I've done so far on the implementation view. I think I'm on the right path, but cannot figure out how to declare the [UIcolor] to the prediction label on the interface page self.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", @"Maybe, yes", nil];

self.predictions = [[NSArray alloc] initWithObjects: @"red", @"green", @"blue",nil]; }

(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

(IBAction)buttonPressed { int random = arc4random_uniform(self.predictions.count); self.predictionLabel.text = [self.predictions objectAtIndex:random]; self.predictionLabel.color = [self.predictions objectAtIndex:random];

5 Answers

I do not have the answer

You are using the same variable name for the predictions and color array. You will have to define another variable name ex. self.colors = [[NSArray alloc] initWithObjects: @"red", @"green", @"blue",nil];

Thanks Rajat, still getting an error though. This is what I have on the interface page

@interface SDViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong, nonatomic) NSArray *predictions; @property (strong, nonatomic) 'UIColor' UILabel *colors;

In the interface add this. @property (strong, nonatomic) NSArray *colorsArray;

And in the implementation now initialize this object self.colorsArray = [[NSArray alloc] initWithObjects: @"red", @"green", @"blue",nil];

Well, i'm still getting all screwed up. Implementation:

[super viewDidLoad];

self.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",
                   @"Maybe, yes", nil];

self.colorsArray = [[NSArray alloc] initWithObjects:
                   @"red",
                   @"green",
                   @"blue",nil];

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

  • (IBAction)buttonPressed { int random = arc4random_uniform(self.predictions.count); self.predictionLabel.text = [self.predictions objectAtIndex:random]; self.predictionLabel.color = [self.predictions objectAtIndex:random];

Interface:

import <UIKit/UIKit.h>

@interface SDViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong, nonatomic) NSArray *predictions; @property (strong, nonatomic) NSArray *colorsArray;

  • (IBAction)buttonPressed;

@end