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

  • Home
  • SEARCH
  • 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 8394059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:57:58+00:00 2026-06-09T19:57:58+00:00

how can we add an object to a NSMutableArray at index 0, when the

  • 0

how can we add an object to a NSMutableArray at index 0, when the first index was previously removed? insertObject does not work :

Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]’

When the screen moves to the right, i remove the first index of the array (which is not visible anymore) and add it to the right. But if i want to go back to the left, i have to add the previously removed image (now at index 2, but the images are the same, so it does not really matter if i just add another image). addObject adds an object at the end of the array, and insertObject throws an error…

Would you know any way to fix this?

linesArray = [[NSMutableArray alloc] initWithObjects:lines1, lines2, lines3, nil];

//left direction : add the object at index 0
if (offsetParam >= offset1){
    CCSprite *temp = [[sprites objectAtIndex:1] retain];
    [sprites removeObjectAtIndex:2];
    [sprites insertObject:temp atIndex:0];//****CRASH HERE****
    temp.position = ccp(-offset1-spriteWidth, temp.position.y);
    [temp release];
    offset1 += spriteWidth;
}

Thanks

  • 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-06-09T19:57:59+00:00Added an answer on June 9, 2026 at 7:57 pm

    If you read that exception a little closer

    Terminating app due to uncaught exception ‘NSRangeException’, reason:
    ‘* -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]’

    You will see that it’s actually a crash on the line right before the one you mention. This line is fine:

    [sprites insertObject:temp atIndex:0];//CRASH HERE****
    

    It the previous one that crashes:

    [sprites removeObjectAtIndex:2];
    

    It looks like you have an array with 2 items, (indices 0 and 1) so removing index 2 crashes because there is no index 2.

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

Sidebar

Related Questions

While we can add Inline Images in paragraphs there does not appear to be
I can not addObject into 'easyQArray' (NSMutableArray). It did work when I had the
We can have an NSMutableArray object, and add objects to it. But CGPoint is
So I know that you can add an object in java to something through
since method Add() takes Object as an argument, can I add for example datatables,
Why should we add an object name to a QObject? I can still load
I know that when we add an object obj into a NSMutableArray, it takes
Suppose you have several Class object declarations and add it into a NSMutableArray, then
At first my table view is empty, and then you can add your own
How can I check to see if an object in an NSMutableArray is nil

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.