我在初始化NSMutableArray
时出现问题_entryArray
与2 NSString
对象@"00:00:00"
。我收到以下错误:用NSString初始化NSMutableArray问题
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
当我尝试在没有任何对象的情况下初始化它时,我没有得到任何错误。我该怎么办?
非常感谢您
代码:
@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];
}
什么声明给你引用的例外? (肯定不是你上面列出的语句之一。) – 2013-03-22 01:25:08
'_timeInBackup = [[NSMutableArray alloc] initWithObjects:(NSNumber *)0,nil];'should be'_timeInBackup = [[NSMutableArray alloc] initWithObjects:@ 0, nil];''或旧的语法'_timeInBackup = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0],nil];'_timeOutBackup'也一样。 – dasblinkenlight 2013-03-22 01:27:19
(但是,如果你使用NSLog创建的数组,你可能会学到一些东西,_timeInBackup和_timeOutBackup不包含你认为它们的值。) – 2013-03-22 01:27:26