user2236306
user2236306

Reputation: 1

App crashing on iPad but not simulator

My Application is crashing when I run it on the iPad but works 100% on the iPad simulator I am using Version 4.6.1 of Xcode and Version 6.1.3 on the iPad. The problem lies where I am trying to pass the value of an int between segues

in my .h

@property (nonatomic, assign)int currentQuestion;

in my .m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"level1correct"]){
    AddLevel1IncorrectViewController *incorrect = [segue destinationViewController];
    incorrect.CQValue = self.currentQuestion;
}}

AddLevel1Incorrect.h

@property (nonatomic, assign)int CQValue;

AddLevel1Incorrect.m

@synthesize CQValue = _CQValue;

- (void)imageSelect{
int numItems = [arrayPath count];
NSMutableArray *left = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:numItems];

for (NSDictionary *itemData in arrayPath) {
    [left addObject:[itemData objectForKey:@"L"]];
    [right addObject:[itemData objectForKey:@"R"]];
}

NSLog(@" value of %d CQValue ", self.CQValue);
leftImageViewer.image = [UIImage imageNamed:left[self.CQValue]];//this is the point where the crash happens
rightImageViewer.image = [UIImage imageNamed:right[self.CQValue]];
}

The interesting thing is it does display the correct value in the NSLog in the Console as you will see at the top of the Crash Message

2013-04-03 22:50:00.404 thefyp[1506:907]  value of 1 CQValue 
2013-04-03 22:50:00.408 thefyp[1506:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:

Any ideas where I am going wrong here?

Upvotes: 0

Views: 193

Answers (1)

Richard Brown
Richard Brown

Reputation: 11434

Your code is very brittle, meaning it makes a lot of assumptions about the data it's working with without verifying the data is accurate.

You never check the bounds of the array before you access it. self.CQValue is 1, but in this case the array itself is empty. so left[self.CQValue], is left[1], which is invalid.

Check arrayPath to make sure it's not empty.

Upvotes: 2

Related Questions