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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:30:17+00:00 2026-05-27T12:30:17+00:00

Overview I am using fast enumeration to iterate through an NSDictionary instance I expected

  • 0

Overview

  • I am using fast enumeration to iterate through an NSDictionary instance
  • I expected the NSDictionary instance to be enumerated based on the ascending order of the key but that doesn’t seem to be the case

What I want to do:

  • I want to be able iterate through the NSDictionary instance in the ascending order of the key using fast enumeration

Note: Pls see expected output vs actual output

Questions

  1. Am i making a mistake with my implementation ?
  2. Does NSDictionary’s fast enumeration guarantee ordering based on keys ?
  3. If not then is there a work around for this and yet use fast enumeration ?

Example

#import<Foundation/Foundation.h>

int main()
{
    system("clear");

    NSDictionary *d1 = nil;

    @autoreleasepool
    {   

        d1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"AAA", [NSNumber numberWithInt:10], 
              @"BBB", [NSNumber numberWithInt:20],               
              @"CCC", [NSNumber numberWithInt:30],               
              nil];
    }   

    for(NSNumber* n1 in d1)     //I expected fast enumeration for NSDictionary to be based on the 
        //ascending order of the key but that doesn't seem to be the case
    {
        printf("key = %p"
               "\t [key intValue] = %i"
               "\t value = %s\n", 
               n1, 
               [n1 intValue], 
               [[d1 objectForKey:n1] UTF8String]);
    }   

    return(0);
}

Expected Output

key = 0xa83      [key intValue] = 10     value = AAA
key = 0x1483     [key intValue] = 20     value = BBB
key = 0x1e83     [key intValue] = 30     value = CCC

Actual Output

key = 0x1e83     [key intValue] = 30     value = CCC
key = 0xa83      [key intValue] = 10     value = AAA
key = 0x1483     [key intValue] = 20     value = BBB
  • 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-27T12:30:18+00:00Added an answer on May 27, 2026 at 12:30 pm
    1. No your implementation is correct.
    2. NSDictionary fast enumeration does not guarantee sorting (and it will not output anything in order because of implementation as hashed container).
    3. No, you have to sort it yourself.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a tab based application that shows a presentModalViewController called overview that
Overview I am using CompositeWPF to create an app using C#. This really should
Overview I am trying to get a photo feed on to my site using
In Eclipse we are using Mylyn to have an overview of our Trac Tickets.
I am using jQuery star rating control found at http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I am using
Overview: I'm working on some windows service using Visual Basic 2010, which deployed on
I am using this plugin. http://jquery.malsup.com/block/#overview However I would like this blockUI to only
As a quick overview: I'm trying to make a game of pool using Box2D
Could someone give me a quick overview of the pros and cons of using
Situation Overview I'm using Ext.NET GridPanel to display a list of projects which has

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.