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 8790873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:48+00:00 2026-06-13T22:45:48+00:00

So this isn’t making sense at all. I have an extension method to NSMutableArray

  • 0

So this isn’t making sense at all. I have an extension method to NSMutableArray to move an item from one index to another. It’s a fairly simple method and it works flawlessly when I compile my app in Debug Configuration. However, if I compile the app in Release Configuration, it crashes if I move from an item down (from index > to index). The crash isn’t a an index out of bounds error. My local variable are being messed up and I have no idea why. Here’s the entire method:

- (void)    moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
    if (fromIndex == toIndex) return; 
    if (fromIndex >= self.count) return; 
    if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
    id movingObject = [self objectAtIndex:fromIndex];

    if (fromIndex < toIndex){
        for (int i = fromIndex; i <= toIndex; i++){
            [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
        }
    } else {
        //The problem occurs in this block (though the crash doesn't always occur here)
        id cObject;
        id prevObject;
        for (int i = toIndex; i <= fromIndex; i++){
            //usually on the last loop, my "prevObject" become 'messed up' after the following line:
            cObject = [self objectAtIndex:i];
            [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
            prevObject = cObject;
        }
    }
}

I can’t step through the code since it’s a release build, but I’ve NSLogged the variables at each step through the loop. Usually, on the last loop the prevObject var is assigned some random variable when cObject = [self objectAtIndex:i]; completes. Sometimes it’s set to nil but often it’s some other random variable in my code. If it’s nil the code crashes when I try to replace the object in the array. Otherwise, it crashes later on when I try to access the array and receive back the wrong object.

Does anyone have any idea what’s going on? I mean, the problem is occurring in 4 lines of code, which I’ve been over a hundred times.

  • 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-13T22:45:49+00:00Added an answer on June 13, 2026 at 10:45 pm

    Ok, just as the problem/crash made no sense, so also the fix doesn’t make any sense at all. I had started to just randomly change up the code to see if anything would work. Moving the line: prevObject = cObject to the very beginning of the loop fixed the problem. Doing this didn’t change the logic at all… Nada… shouldn’t have made a difference. And yet, it did. Whoever said programming is logical? Here’s the code that works:

    - (void)    moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
        if (fromIndex == toIndex) return; 
        if (fromIndex >= self.count) return; 
        if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
        id movingObject = [self objectAtIndex:fromIndex];
    
        if (fromIndex < toIndex){
            for (int i = fromIndex; i <= toIndex; i++){
                [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
            }
        } else {
            id cObject = nil;
            id prevObject;
            for (int i = toIndex; i <= fromIndex; i++){
                prevObject = cObject;
                cObject = [self objectAtIndex:i];
                [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
            }
        }
    }
    

    So, anybody want to chime in as to what’s going on?

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

Sidebar

Related Questions

This isn't something that I expected from the asset pipeline. Let's say I have
This isn't about a specific problem I'm having, but I have to clear something
This isn't homework for me , it's a task given to students from some
This isn't a question on whether it's safe to throw an exception from a
This isn't quite as straight forward as one may think. I'm using a plugin
This isn't another one of those How can I record audio in the browser?
This isn't working as I'd like. I have an entry form for a new
(This isn't my program, but I'll try to provide all the relevant information to
This isn't working: <?php header('Location: www.mysite.com/index.php?foo=bar&var=abc'); ?> I end up with www.mysite.com/index.php?foo=bar I think
This isn't a real fluent interface. I have an object which builds up a

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.