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

STILL struggling with Crystal Ball extra credit

The assignment asks that we make an NSArray of [UIColor] objects and change the color of the predictionLabel text randomly when the button pressed method is called. I hired a tutor (which I can't afford) who came up with the last 2 lines below in my implementation page but I can only pull up red and green colors and I don't know why. I guess I don't need to make a new property in my Interface page, but I don't know why not. I'd really like to SEE an EXAMPLE of the correct code please.

Here's what he added to the Implementation page Last 2 lines :

  • (IBAction)buttonPressed {

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

    self.predictionLabel.textColor= [UIColor colorWithRed:(arc4random_uniform(1)) green:(1) blue:(1) alpha:1];

4 Answers

I created an NSArray property in the View Controller called predictionColors;

In the viewDidLoad method, I initialized the array with:

- (void)viewDidLoad {
     self.predictionColors = [[NSArray alloc] initWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor blueColor],
                             [UIColor cyanColor],[UIColor purpleColor],[UIColor grayColor],[UIColor orangeColor],[UIColor magentaColor],
                             [UIColor brownColor],nil];
}

In the makePrediction method in the View Controller, I used the following to get the random colors:

- (void)makePrediction {
    NSInteger randomColorIndex = arc4random()%[self.predictionColors count];
    self.predictionLabel.textColor = [self.predictionColors objectAtIndex:randomColorIndex];
}

It may not be the proper way, but it worked well for me.

Hi Steve!

Thank you so much for your help! I thought the assignment asked for an NSArray in the "button pressed method" He never showed us anything called makePrediction. I'm still so lost. Thinking of starting at the beginning and taking the intro to programming course. Thank you anyway.

No problem! It depends on where you are at in the assignment. Initially you start out with a button, and the button pressed method would generate the prediction. Later in the assignment the code is refactored into a standalone method "makePrediction" which is called upon by both a motion gesture (shaking the phone) or by touch.

If you are using the button and buttonPressed method to generate a prediction, you can combine the above statements into one method to generate the different colors.

- (IBAction)buttonPressed {
    NSArray *predictonColors = [[NSArray alloc] initWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor blueColor],
                             [UIColor cyanColor],[UIColor purpleColor],[UIColor grayColor],[UIColor orangeColor],[UIColor magentaColor],
                             [UIColor brownColor],nil];

    NSInteger randomColorIndex = arc4random()%[predictionColors count];
    self.predictionLabel.textColor = [predictionColors objectAtIndex:randomColorIndex];
}

You'll want to eventually refactor the above code so that the predictionColors is initialized in the viewDidLoad method and not each time the button is pressed for better memory management, but the above code should get you the random colors :).

Omar Harris
Omar Harris
1,052 Points

I am working on this same extra credit code challenge Steve Livingston . Having a memory allocation issue? I chose to make color a property.

here is my property i created

@property (strong, nonatomic) NSArray *colors;

here is my implementation under -(void)[viewDidLoad]

 self.colors = [[NSArray alloc]initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], [UIColor orangeColor],  nil];

here is my implementation under [buttonPressed]

int randColors = arc4random_uniform(self.colors.count); self.predictionLabel.text = [self.colors objectAtIndex:randColors];

any issues with my code? Apologies I haven't figured out how to make those black code snippets in the comments like you guys did.

You just missed one simple thing

The changing method needs to be "self.predictionLabel.textColor" not "self.predictionLabel.text"

Your final implementation should be...

int randColors = arc4random_uniform(self.colors.count); self.predictionLabel.textColor = [self.colors objectAtIndex:randColors];