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
Christian Binamira
Courses Plus Student 2,277 PointsCrystalBall randomColors Extra Credit
Alright so for this Extra Credit, I decided to use the CrystalBall class that we created in the previous videos and store the information for the NSArray randomColor, like we did for randomPrediction. I basically just wrote the same code we did for randomPrediction, but changed it so it would only affect the text color. When I run the program, it works fine, but as soon as I click the predict button it tells me Thread 1: break 1 on this line in the (IBAction)buttonPressed Implementation in THViewController
self.predictionLabel.text = [self.crystalBall randomPrediction];
Here's my code for the THCrystalBall.h
@interface CBCrystalBall : NSObject {
NSArray *_predictions;
NSArray *_colors;
}
@property (strong, nonatomic, readonly) NSArray *predictions;
@property (strong, nonatomic, readonly) NSArray *colors;
- (UIColor*) randomColor;
- (NSString*) randomPrediction;
@end
Code for THCrystalBall.m for randomColors and color
- (NSArray *) colors {
if (_colors == nil) {
_colors = [[NSArray alloc] initWithObjects: [UIColor greenColor], [UIColor redColor], [UIColor blueColor], [UIColor yellowColor], nil];
}
return _colors;
}
- (UIColor*) randomColor {
int random = arc4random_uniform(self.colors.count);
return [self.colors objectAtIndex:random];
}
- (IBAction)buttonPressed {
self.predictionLabel.text = [self.crystalBall randomPrediction];
self.predictionLabel.textColor = [self.crystalBall randomColor];
}
4 Answers
Stone Preston
42,016 Pointsmaybe you set a breakpoint. try pressing command + y to deactivate all breakpoints and then try running again. if that doesnt work, there should be some information about the exception that was thrown in the console. scroll up and down in the console after your app crashes and see if you can find the reason the exception was thrown
Christian Binamira
Courses Plus Student 2,277 PointsWhat exactly is a breakpoint? If activated one somehow, I do not know how.
Stone Preston
42,016 Pointsit sets a point in your code where execution stops and you can step through your code manually line by line. it sounds like thats what you did since the error mentions break. you can add breakpoints accidently by clicking in the gutter in the editor near the line numbers. It looks like a little flag shaped rectangle thing
you can also view any added breakpoints by pressing command + 7 to bring up the breakpoint navigator
Christian Binamira
Courses Plus Student 2,277 PointsThat fixed it! Thank you so much! I stayed up late looking through my code and see where I went wrong, and I can't believe it was just that, haha! Again, thank you!!!
Bjoern Sommer
3,211 PointsI basically had the same Idea as Christian but instead of using the CrystalBall class, I wanted to put it in its own "Colors" class. The code itself is the same as above, but I keep getting the error "Use of undeclared identifier 'UIColor'" inside of the colors Array. Later I followed the example from above and copied Christians code to my app, but I still get that same 'UIColor' error. Is there something I'm missing here?
Thank you in advance!