【发布时间】:2011-10-30 06:52:28
【问题描述】:
我假设在我的代码中的某处,数组中的对象被删除的速度比创建它们的速度要快。我一直在寻找问题一个多小时,所以请帮忙!
这里是错误:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
提前致谢,这是我的代码:
- (void)onTimer {
UIImageView *imgView = [[UIImageView alloc] init];
imgView.image = particleImg;
imgView.frame = CGRectMake(kViewDimensions/2, kViewDimensions/2, startSize.width, startSize.height);
[self addSubview:imgView];
[particlesArray addObject:imgView];
[imgView release];
moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveObjs) userInfo:nil repeats:YES];
}
- (void)moveObjs {
for (int i = 0; i < [particlesArray count]; i++) {
animID = @"MoveId";
UIImageView *imgV = [particlesArray objectAtIndex:i];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGPoint randPoint = CGPointMake(arc4random()%kMaxRandX, arc4random()%kMaxRandY);
imgV.center = CGPointMake(randPoint.x, randPoint.y);
[UIView commitAnimations];
animValue = i;
num = [NSNumber numberWithInt:animValue];
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(animationEnded) userInfo:num repeats:NO];
NSLog(@"\n %d",animValue);
}
}
- (void)animationEnded {
int av = [num intValue];
UIImageView *iv = [particlesArray objectAtIndex:av];
if ([animID isEqualToString:@"MoveId"]) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
iv.alpha = 0.0f;
[UIView commitAnimations];
[particlesArray removeObjectAtIndex:av];
}
else {
[iv removeFromSuperview];
}
}
【问题讨论】:
标签: iphone arrays memory nsmutablearray nsarray