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
Matthew Waller
1,698 PointsCrystal Ball fortunes one at a time?
Hi, I'm wondering, is there a way to make the crystal ball fortunes appear one at a time until the array is exhausted, and then have the fortunes start over again? I experimented with
-
(void) makePrediction {
for(int index = 0;index<self.predictionArray.count;index++) {
self.predictionLabel.text =[self.predictionArray objectAtIndex:index];//NSUInteger index = arc4random_uniform(self.predictionArray.count);
[self.imageView startAnimating];
[UIView animateWithDuration:2.0 animations:^{
self.predictionLabel.alpha = 1.0;}]; } }
However, all this seems to do is go to the last value of the array, naturally enough.
Any tips on how to make this happen? To display one value of the array with each touch, and then the next until the array is done, and then start over again?
Thanks for any help!
2 Answers
Aaron Daub
Courses Plus Student 389 PointsYou're going to need to keep track of the current index. Add a property:
@property (nonatomic, readwrite, assign) NSUInteger currentIndex;
Now change the makePrediction method to be the following:
-(void)makePrediction {
self.predictionLabel.text = self.predictionArray[self.currentIndex % self.predictionArray.count];
self.currentIndex += 1;
}
Why does this work? Numerical values begin at 0, so the initial value of self.currentIndex is 0. After you hit the button once the value is 0 + 1 = 1. If you hit the button twice it'll be 0 + 1 + 1 = 2. Therefore the value of the button will be equal to the number of times you hit the button. Let's call that number n.
Let's call the amount of predictions you have in the array x. We access the prediction at index n % x. If you don't know what % does, it takes the remainder of n divided by x. So if n = (cx + y) then n % x = y. This means that no matter how many times you hit the button, you'll never try to access an element outside of the array (so long as it has at least one element. n % 0 is undefined because n % 0 requires division by 0 to be defined).
Kyle Johnson
33,528 PointsI'm not on my on Xcode so I'm writing this post from memory but I believe at the end of the for loop you will need to removeObjectAtIndex and in order to use the removeObjectAtIndex method you will need to change your NSArray to a NSMutableArray. You should also use a while loop.
while (predictionArray.count > 0) {
NSUInteger index = arc4random_uniform(self.predictionArray.count);
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];
[predictionArray removeObjectAtIndex: index];
}
Kyle Johnson
33,528 PointsKyle Johnson
33,528 PointsI'm not on my on Xcode so I'm writing this post from memory but I believe at the end of the for loop you will need to removeObjectAtIndex and in order to use the removeObjectAtIndex method you will need to change your NSArray to a NSMutableArray. You should also use a while loop.
Matthew Waller
1,698 PointsMatthew Waller
1,698 PointsHey thanks Aaron! It works great! And nice explanation.