xCode 线程 1:EXC_Arithmetic(code=EXC_1386_DIV,subcode=0x0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26554340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:59:21  来源:igfitidea点击:

xCode Thread 1:EXC_Arithmetic(code=EXC_1386_DIV,subcode=0x0)

iosobjective-cxcodeexceptionarc4random

提问by Mike1360

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.

出现上述错误导致我的 iOS 应用程序崩溃。以前从未见过这个,想知道是否有人可以帮助我?我是编码和使用我的第一个应用程序的新手,而没有遵循课程/教程。

-(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;

它出现在第二行 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.

我拥有的是一个名为 mainImages 的数组。
该数组包含具有名为 mainTextImage 的 UIImage 属性的对象。在此方法中,我试图将 mainTextImage 设置为等于 mainImageView.image,以便在调用此方法时,随机选择其中一个图像显示在 mainImageView 中。

Thanks!

谢谢!

采纳答案by latenitecoder

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?

为了完成这项工作,我需要知道 Main Image 类是/做什么,还是只想用图像填充数组并返回它?

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. :)

那些必须在你的代码上工作的人会感谢你的清晰。:)

回答by latenitecoder

This line

这条线

int numberOfImages = (int)[self.mainImages count];

Should be

应该

int numberOfImages = [self.mainImages count];

I'm presuming self.mainImages is an array object and has been initialised and populated. Put a breakpoint on your crashing line and check. The error looks like a divide by zero error to me.

我假设 self.mainImages 是一个数组对象并且已经初始化和填充。在崩溃的行上放置一个断点并检查。对我来说,这个错误看起来像是除以零错误。

EDIT:

编辑:

I knocked this up to show you have an issue elsewhere. Definitely double check the array has values.

我敲这个是为了表明你在其他地方有问题。一定要仔细检查数组是否有值。

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);
}

This is the debug output:

这是调试输出:

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

Let me know if this helps

让我知道这是否有帮助

回答by latenitecoder

Ok here's what you need.

好的,这就是你需要的。

- (NSArray *)mainImages {
    NSArray *mainImages = [[NSArray alloc] initWithObjects:
                            [UIImage imageNamed:@"purpleText.png"]
                           ,[UIImage imageNamed:@"whiteText.png"]
                           ,[UIImage imageNamed:@"blackText.png"]
                           ,nil];
    return mainImages;
}

Have a good night

祝你有一个美好的夜晚