簡體   English   中英

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

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

得到上面的錯誤,導致我的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;出現在第二行int randomIndex = arc4random() % numberOfImages;

我所擁有的是一個稱為mainImages的數組。
該數組包含具有UIImage屬性(稱為mainTextImage)的對象。 在此方法內部,我試圖將mainTextImage設置為與mainImageView.image相等,以便在調用此方法時,將隨機選擇其中一個圖像以顯示在mainImageView中。

謝謝!

這條線

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

讓我知道這是否有幫助

答對了! -閱讀評論

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

必須親自處理您的代碼的人員將感謝您的清晰度。 :)

好的,這就是您所需要的。

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

祝你有一個美好的夜晚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM