获取上述错误导致我的iOS应用崩溃。从来没有见过这个,并想知道是否有人可以帮助我?我是新手编写代码而没有上过课程/教程的第一个应用程序。
-(void)updateGamePlay {
int numberOfImages = (int)[self.mainImages count];
int randomIndex = arc4random() % numberOfImages;
MainImage *imageView = [self.mainImages objectAtIndex:randomIndex];
self.mainImageView.image = imageView.mainTextImage;
}
它出现在第二行
int randomIndex = arc4random() % numberOfImages;
我所拥有的是一个名为mainImages的数组 该数组包含具有UIImage属性的对象,称为mainTextImage。 在这个方法中,我试图将mainTextImage设置为等于mainImageView.image,以便在调用此方法时,随机选择其中一个图像以显示在mainImageView中。
谢谢!
答案 0 :(得分:0)
这一行
int numberOfImages = (int)[self.mainImages count];
应该是
int numberOfImages = [self.mainImages count];
我假设self.mainImages是一个数组对象,已经初始化和填充。在崩溃线上放置一个断点并检查。该错误看起来像是一个零除错误。
编辑:
我敲了这个以表明你在其他地方有问题。绝对仔细检查数组是否有值。
NSMutableArray *testQuicklyArray = [[NSMutableArray alloc]init];
UIImage *myImage = [[UIImage alloc]init];
for (int i = 0; i < 100 ; i++)
{
[testQuicklyArray addObject:myImage];
}
self.mainImages = [testQuicklyArray copy];
for (int i = 0; i < 10; i++)
{
int numberOfImages = [self.mainImages count];
int randomIndex = arc4random() % numberOfImages;
// MainImage *imageView = [self.mainImages objectAtIndex:randomIndex];
// self.mainImageView.image = imageView.mainTextImage;
NSLog(@"Selected Random Image %i", randomIndex);
}
这是调试输出:
2014-10-24 21:04:48.276 StackPlayground[25831:6132347] Selected Random Image 33
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 73
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 65
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 62
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 53
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 15
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 82
2014-10-24 21:04:48.277 StackPlayground[25831:6132347] Selected Random Image 70
2014-10-24 21:04:48.278 StackPlayground[25831:6132347] Selected Random Image 26
2014-10-24 21:04:48.278 StackPlayground[25831:6132347] Selected Random Image 50
如果有帮助,请告诉我
答案 1 :(得分:0)
宾果! - 阅读评论
- (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
}
为了完成这项工作,我需要知道Main Image类是什么/做什么,或者你只是想用图像填充数组并返回它?
另外
此
MainImage *purpleText = [[MainImage alloc] init];
purpleText.mainTextImage = [UIImage imageNamed:@"purpleText.png"];
是一个混乱的命名约定。图像和文字这两个词的使用相互冲突。我将这些东西重命名为它的名称,即
MainImage *purpleTextImage = [[MainImage alloc] init];
purpleTextImage.mainTextImage = [UIImage imageNamed:@"purpleTextImage.png"];
那些必须处理您的代码的人会感谢您的清晰度。 :)
答案 2 :(得分:0)
好的,这就是你需要的。
- (NSArray *)mainImages {
NSArray *mainImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"purpleText.png"]
,[UIImage imageNamed:@"whiteText.png"]
,[UIImage imageNamed:@"blackText.png"]
,nil];
return mainImages;
}
度过一个美好的夜晚