Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:18:25+00:00 2026-05-24T17:18:25+00:00

I am assuming that somewhere in my code the objects in the array are

  • 0

I am assuming that somewhere in my code the objects in the array are being removed faster than they are created. I have been looking for the problem for over an hour so please help!

Here is the error:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
Thanks in advance and here is my code:

- (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];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:18:26+00:00Added an answer on May 24, 2026 at 5:18 pm

    Attempting to trigger a method at the end of your animation with a timer is ill-advised. Instead, you should be using the setAnimationDidStopSelector: class method within your animation block. On top of that, because of the way you are using num to handle removing objects from the array, you are guaranteed to exceed the bounds of the array at some point during one of the invocations of animationEnded. It would be better to reference the object itself, rather than its position in the array. This is what the context parameter in the beginAnimations:context: method is for:

    - (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];
        }
    }
    

    NOTE: If you are not supporting versions prior to iOS 4, then you should switch to using the new Blocks based methods, such as animateWithDuration:animations:completion:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that best practices have been followed when designing a new database, how does
Assuming that I have a CronTriggerBean similar to <bean id=midMonthCronTrigger class=org.springframework.scheduling.quartz.CronTriggerBean> <property name=jobDetail ref=reminderJobDetail
Assuming that I have a typedef declared in my .h file as such: typedef
I have a section of code in a controller that replaces existing HTML with
I have been learning Objective-C as my first language and understand Classes, Objects, instances,
I thought I read somewhere that the WebRole runs in a different process than
Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain
Assuming that I want to use a hash as an ID instead of a
assuming that I know the PID of a process and want to do a
Assuming that I know there is a git-daemon running at git://git.mycompany.com , how can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.