Getting the error above causing my iOS app to crash. Never seen this before and was wondering if anyone could help me? I'm new to coding and taking on my first app without following lessons/tutorials.
-(void)updateGamePlay {
int numberOfImages = (int)[self.mainImages count];
int randomIndex = arc4random() % numberOfImages;
MainImage *imageView = [self.mainImages objectAtIndex:randomIndex];
self.mainImageView.image = imageView.mainTextImage;
}
It appears on the second line with the
int randomIndex = arc4random() % numberOfImages;
What i have is an array called mainImages.
That array holds objects which have a property of UIImage called mainTextImage.
Inside this method, I am trying to set mainTextImage equal to mainImageView.image so that when this method is called, one of those images are randomly selected to be displayed in the mainImageView.
Thanks!
Bingo! - Read the comments
- (NSArray *)mainImages {
MainImage *purpleText = [[MainImage alloc] init];
purpleText.mainTextImage = [UIImage imageNamed:@"purpleText.png"];
MainImage *whiteText = [[MainImage alloc] init];
whiteText.mainTextImage = [UIImage imageNamed:@"whiteText.png"];
MainImage *blackText = [[MainImage alloc] init];
blackText.mainTextImage = [UIImage imageNamed:@"blackText.png"];
NSArray *mainImages = [[NSArray alloc] init]; // Here is where you create the array
return mainImages; // Here is where you return the newly created empty array
}
To make this work I will need to know what the Main Image class is / does or do you just want to fill the array with images and return it?
Also
This
MainImage *purpleText = [[MainImage alloc] init];
purpleText.mainTextImage = [UIImage imageNamed:@"purpleText.png"];
is a mega confusing naming convention. The use of the words image and text are clashing with each other. I'd rename this stuff to mean what it's named i.e.
MainImage *purpleTextImage = [[MainImage alloc] init];
purpleTextImage.mainTextImage = [UIImage imageNamed:@"purpleTextImage.png"];
The people who have to work on your code down the line will thank you for the clarity. :)