Take the 2-minute tour ×

Whenever I click the spin it generates this error and crashes the app. Before I upload the image view the app works fine and spin's great. I know the code is probably telling me head on whats wrong but I am having problems understanding this.

Stated below in bold is what its generating

- (IBAction)spinAction:(id)sender {

    [_chosenComponents removeAllObjects];
    for (int i = 0; i < _componentsInPicker.count; i++) {
        int randNum = arc4random() % [self.theSlotPicker numberOfRowsInComponent:i];

        [_theSlotPicker selectRow:randNum inComponent: i animated:YES];
        [_chosenComponents addObject:
         [NSNumber numberWithInt:[(NSArray *)[_baseList objectAtIndex:i] indexOfObject:[(NSArray *)[_componentsInPicker objectAtIndex:i] objectAtIndex:randNum]]]];
    NSLog(@"%@", _chosenComponents);

Spitting out after button is pressed/crash "Thread 1: EXC_ARITHMETIC (code=EXC_1386_DIV,subcode=0x0)"

If you could please guide me on how to fix this error.

Thank you!

Output error: (lldb)

share|improve this question
please post the error message that the app spits out to xcode's output window. –  John Mar 15 '13 at 18:26
Putting **<bold words>** inside a code block won't make them bold. Put the error separately so that others notice it. –  Rakesh Mar 15 '13 at 18:43
Alright sorry about that newer to this. No such luck with below. Prior to adding the UIImageView it works like a charm. –  user2175105 Mar 15 '13 at 18:47
@user2175105: try logging [self.theSlotPicker numberOfRowsInComponent:i]; inside the loop. Find out on which iteration it's happening. Also log , self.theSlotPicker and see if it's nil? –  Rakesh Mar 16 '13 at 6:29
add comment

1 Answer

This error is occurring probably because there is a division by zero happening when you are using arc4random() % .....

Instead of using arc4random() % n use arc4random_uniform(n). It will return a number in the range of 0 to n.

However this won't solve the inherent problem that

[self.theSlotPicker numberOfRowsInComponent:i] is returning zero at some point of time.

share|improve this answer
add comment

Your Answer


