Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在我的代码中,是什么导致了这个错误(NSMutableArray对于被调用的对象来说太小了)?_Iphone_Arrays_Memory_Nsmutablearray_Nsarray - Fatal编程技术网

Iphone 在我的代码中,是什么导致了这个错误(NSMutableArray对于被调用的对象来说太小了)?

Iphone 在我的代码中,是什么导致了这个错误(NSMutableArray对于被调用的对象来说太小了)?,iphone,arrays,memory,nsmutablearray,nsarray,Iphone,Arrays,Memory,Nsmutablearray,Nsarray,我假设在我的代码中的某个地方,数组中的对象被删除的速度比它们被创建的速度要快。我已经找了一个多小时的问题,所以请帮助 以下是错误:***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSMutableArray objectAtIndex:]:索引7超出边界[0..6]” 提前感谢,以下是我的代码: - (void)onTimer { UIImageView *imgView = [[UIImageView alloc] init]; im

我假设在我的代码中的某个地方,数组中的对象被删除的速度比它们被创建的速度要快。我已经找了一个多小时的问题,所以请帮助

以下是错误:
***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSMutableArray objectAtIndex:]:索引7超出边界[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];
    }
}
-(void)onTimer{
UIImageView*imgView=[[UIImageView alloc]init];
imgView.image=particleImg;
imgView.frame=CGRectMake(kViewDimensions/2,kViewDimensions/2,startSize.width,startSize.height);
[自添加子视图:imgView];
[particlesArray addObject:imgView];
[imgView发布];
moveTimer=[NSTimer scheduledTimerWithTimeInterval:0.05目标:自选择器:@selector(moveObjs)userInfo:nil repeats:YES];
}
-(void)moveObjs{
对于(int i=0;i<[particlesArray count];i++){
animID=@“MoveId”;
UIImageView*imgV=[particleArray objectAtIndex:i];
[UIView beginAnimations:nil上下文:NULL];
[UIView设置动画持续时间:0.3];
CGPoint randPoint=CGPointMake(arc4random()%kMaxRandX,arc4random()%kMaxRandY);
imgV.center=CGPointMake(randPoint.x,randPoint.y);
[UIView委员会];
数值=i;
num=[NSNumber numberwhithint:animValue];
[NSTimer scheduledTimerWithTimeInterval:0.3目标:自选择器:@selector(animationEnded)userInfo:num repeats:NO];
NSLog(@“\n%d”,数值);
}
}
-(void)animationEnded{
int av=[num intValue];
UIImageView*iv=[particleArray对象索引:av];
if([animID isEqualToString:@“MoveId”]){
[UIView beginAnimations:nil上下文:NULL];
[UIView设置动画持续时间:0.1];
iv.α=0.0f;
[UIView委员会];
[particlesrayremoveObjectatindex:av];
}
否则{
[iv从SuperView移除];
}
}

在animationEnded中,我相信您尝试删除图像7次

编辑:更多信息

在moveObjs中循环遍历每个图像(0…n),在每次迭代中使用新值重新分配“num”。所以在上一次迭代中(基于错误,我假设I=7,总共8张图像),num保留为7


当动画结束时,将调用animationEnded,num变回int(av),并删除索引“av”处的元素。

如果在
animationEnded
方法中像这样放置
NSLog

- (void)animationEnded 
{   
    int av = [num intValue];
    NSLog(@"index => %i", av);

    // more stuff
}
您将看到,对于每次运行,它将注销循环中的最大值,即7


因此,第一次在索引7处删除对象是可以的,但是在第二次删除索引7时,如果只有[0..6]项,则尝试在动画结束时使用计时器触发方法是不明智的。相反,应该在动画块中使用
setAnimationDidStopSelector:
class方法。最重要的是,由于您使用num处理从数组中移除对象的方式,因此在调用animationEnded的过程中,您一定会在某个点超出数组的边界。最好是引用对象本身,而不是它在数组中的位置。这就是
beginAnimations:context:
方法中的上下文参数的作用:

- (void)moveObjs {
    for (int i = 0; i < [particlesArray count]; i++) {
        animID = @"MoveId";

        UIImageView *imgV = [particlesArray objectAtIndex:i];

        [UIView beginAnimations:nil context:imgV];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

        CGPoint randPoint = CGPointMake(arc4random()%kMaxRandX, arc4random()%kMaxRandY);
        imgV.center = CGPointMake(randPoint.x, randPoint.y);

        [UIView commitAnimations];
    }
}

- (void)animationDidStop:(NSString *)animID finished:(NSNumber *)finished context:(void *)context {   
    UIImageView *iv = (UIImageView *)context;

    if ([animID isEqualToString:@"MoveId"]) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.1];
        iv.alpha = 0.0f;
        [UIView commitAnimations];
        [particlesArray removeObject:iv];
    } else {
        [iv removeFromSuperview];
    }
}
-(void)moveObjs{
对于(int i=0;i<[particlesArray count];i++){
animID=@“MoveId”;
UIImageView*imgV=[particleArray objectAtIndex:i];
[UIView beginAnimations:nil context:imgV];
[UIView设置动画持续时间:0.3];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
CGPoint randPoint=CGPointMake(arc4random()%kMaxRandX,arc4random()%kMaxRandY);
imgV.center=CGPointMake(randPoint.x,randPoint.y);
[UIView委员会];
    }
}
-(void)animationDidStop:(NSString*)animID finished:(NSNumber*)finished context:(void*)context{
UIImageView*iv=(UIImageView*)上下文;
if([animID isEqualToString:@“MoveId”]){
[UIView beginAnimations:nil上下文:NULL];
[UIView设置动画持续时间:0.1];
iv.α=0.0f;
[UIView委员会];
[particles数组移除对象:iv];
}其他{
[iv从SuperView移除];
    }
}

注意:如果您不支持iOS 4之前的版本,那么您应该切换到使用新的基于块的方法,例如
animateWithDuration:animations:completion:

哦,我忘记了包括这一行代码,对此我很抱歉,但我忘记了包括这一行代码:-(void)startEmitting{generateTimer=[NSTimer scheduledTimerWithTimeInterval:0.05目标:自选择器:@selector(onTimer)userInfo:nil repeats:YES];}尽管如此,问题仍然存在于'num'的赋值和元素的删除中-您总是删除元素#7,如Paul.s的回答所示。好的-我没有看到您通过'num'-尽管如此,删除元素还是有问题…我将查找一些内容,然后更新我的答案。非常感谢,但我看到了您所写的内容这是真的,你知道我将如何去修复它,因为根据计时器的不同,它们的间隔可以是不同数量的总图像吗