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

UIColor arrays

I don't quite understand how to create a UIColor array. So far, I've added @property (strong, nonatomic) NSArray *colorArray; to my ViewController.h file and synthesized it in the implementation file.

Then in the viewDidLoad method I wrote self.colorArray = [[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor greenColor], nil];

Now I'm stuck though I'm trying to use something similar to the text randomization feature from the crystal ball app, but it's not working. So what I've got is

NSUInteger color = arc4random_uniform(self.colorArray.count);
    self.colorArray.????? = [self.colorArray objectAtIndex:color];

but I don't know what comes just to the right of self.colorArray. Thanks for any help!

3 Answers

I'm not sure what your goal is here. The code above makes an array of UIColors (colorArray) and a random number (color). So your [self.colorArray objectAtIndex:color] successfully returns a random color. But what I don't understand is what you're trying to do on the left side of the equals. I don't think you want to do anything to colorArray. You can apply your returned color to a view. You can output your returned color as a log. But you don't want to declare anything in colorArray anymore.

You could set your random color to a specific index of your array if that's what you're going for. self.colorArray[0] = [self.colorArray objectAtIndex:color]

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The right side is fine. What you want to do is access a random object from the array and assign it something. In your example you are accessing an object and assigning it back to the array. That's not what you want to do. You can assign to the text color of the label like this:

NSUInteger color = arc4random_uniform(self.colorArray.count);
self.predictionLabel.textColor = [self.colorArray objectAtIndex:color];

Ok. Thanks to both of you! I fixed the problem.