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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:48:55+00:00 2026-05-31T01:48:55+00:00

I have a Mutable Dictionary that I removed an element from with removeObjectForKey. That’s

  • 0

I have a Mutable Dictionary that I removed an element from with removeObjectForKey. That’s fine, but when I enumerate thru the Dictionary, there is a ‘hole’ for the element I deleted. So when I print the Dictionary, it displays that element as (null).

Is there way to ‘pack’ the dictionary after removing an element? I need contiguous numbers for the keys. Example:

BEFORE DELETE:

key:1 value:red
key:2 value:green
key:3 value:blue
key:4 value:yellow

myDictionary removeObjectForKey:2

CURRENT:

key:1 value:red
key:3 value:blue
key:4 value:yellow

DESIRED:

key:1 value:red
key:**2** value:blue
key:**3** value:yellow

Code to remove nil entry from NSMutableDictionary. This is what I came up with, but it is not working:

int count = dictFaves.count;
int x = 1;  // Dictionaries are 1-relative
while ( x <= count ) {

   // get the current row
   NSString *curRow = [NSString stringWithFormat:@"%d", x];
   NSString *temp = [dictFaves objectForKey:curRow];

   // is this row empty? if so, we have found our hole to plug
   if ( temp == nil ) {
       // copy the Fave from the 'next' row to the 'current' row. Effectively   
       //   shifting it 1 lower in the Dictionary
       NSString *nextRow = [NSString stringWithFormat:@"%d", x + 1];
       temp = [dictFaves objectForKey:nextRow];
       [dictFaves setObject:temp forKey:[NSNumber numberWithInt:x]];

       // one final thing to cleanup: remove the old 'next' row. 
       // It has been moved up 1 slot (along with all others)
       [dictFaves removeObjectForKey:[NSString stringWithFormat:@"%d", x+1]];
   }
   x = x + 1;
}
  • 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-31T01:48:56+00:00Added an answer on May 31, 2026 at 1:48 am

    That happens because the NSDictionary (and its mutable subclass) acts like a hash/map/associative array. If you want to keep the indices running consecutively, you have to either reset them after removing an object, or just store everything in a NSMutableArray.

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

Sidebar

Related Questions

Is there a way to have a mutable static variable in F# class that
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I have a mutable array which stores sprites but the problem is that the
I have a mutable class that I'm using as a key to a generic
I have a mutable array that is retained and storing several objects. At some
I have a question about the difference between volatile and mutable. I noticed that
I have an application that downloads information from a web service and caches it
I have a mutable array that has been retained. This array contain dictionaries with
If you have a method in objective c that builds an array or dictionary
I have a dictionary/list balance_sign_dict from which I need to retrieve 2 values based

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.