Reputation: 3067
I'm having problem initialising an NSMutableArray
called _entryArray
with 2 NSString
objects @"00:00:00"
. I'm getting the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
When I try to initialise it without any objects in it, I get no errors. What do I do?
Thank you very much
code:
@implementation MainViewController{
@private
int n;
NSMutableArray *_entryArray;
NSMutableArray *_timeSinceLastEntryArray;
NSMutableArray *_timeInterval;
NSMutableArray *_timeInBackup;
NSMutableArray *_timeOutBackup;
BOOL whichButton;
}
-(void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidLoad
{
[super viewDidLoad];
n=0;
_brain = [[Brain alloc] init];
_entryArray = [[NSMutableArray alloc] initWithObjects:@"00:00:00",@"00:00:00",nil];
_timeSinceLastEntryArray = [[NSMutableArray alloc] init];
_timeInBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeInterval = [[NSMutableArray alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
Upvotes: 0
Views: 117
Reputation: 318824
These two lines result in arrays with zero objects:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0, nil];
You are not adding the number 0, you are adding the pointer 0. This is the same as nil
. In other words, this code is really this:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:nil, nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:nil, nil];
If your intent is to add the number 0, do this:
_timeInBackup = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
_timeOutBackup = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
Better yet, do this:
_timeInBackup = [@[ @0 ] mutableCopy];
_timeOutBackup = [@[ @0 ] mutableCopy];
You also have other empty arrays:_timeSinceLastEntryArray
and _timeInterval
. None of the code you have posted is actually causing the error. You need to provide more details to know for sure which array is really causing you the problem.
Upvotes: 4