0

How can I ensure the position of key-value pairs inside a NSDictionary when defining it?

For example if I define:

[detailsView setDatasource: @[ 
   @[@"Details",
      @{ @"title_1":@"description1",
         @"title_2":@"description2",
         @"title_3":@"description3",
         @"title_4":@"description4" }],
   @[@"Actions",
      @{ @"Action_1":[NSNull null],
         @"Action_2":[NSNull null],
         @"Action_3":[NSNumber numberWithBool:false] }]
];

Then at runtime I have different order of items

[detailsView setDatasource: @[ 
   @[@"Details",
      @{ @"title_2":@"description2",
         @"title_3":@"description3",
         @"title_4":@"description4",
         @"title_1":@"description1" }],
   @[@"Actions",
      @{ @"Action_3":[NSNumber numberWithBool:false],
         @"Action_1":[NSNull null],
         @"Action_2":[NSNull null]}]
];

This behaviour is unpredictable... How NSDictionary is initialised when initialising it with literals , and is there a way of ensure that a key-value pair will be at defined position when using literals ?

-- EDIT --

Not a brilliant solution but works. I ended-up doing like this:

@[
    @[ @"Route details",
         @[
              @[@"route-direction", @"Route direction", [NSString stringWithFormat:@"%@ → %@", journey[@"from"], journey[@"to"]]],
              @[@"poi-count", @"Points of interest", journey[@"pois-count"]],
              @[@"date-created", @"Date created", journey[@"created-at"]],
          ]
     ],
    @[ @"Actions" ,
         @[
             @[@"favourite", @"Favourite", ( [journey objectForKey:@"favourite"] ? [journey objectForKey:@"favourite"] : [NSNumber numberWithBool:false])],
             @[@"load",@"Load now",[NSNull null]],
             @[@"delete",@"Delete",[NSNull null]],
          ]
     ],
[NSNumber numberWithInteger: indexPath.row]]];
Svenv
  • 369
  • 1
  • 3
  • 14
  • you question make no point as long as you will access to the _values_ by the _keys_ only (`–valueForKey:` or `–valueForKeyPath:`), so the actual order of the elements are really irrelevant. what do you like to achieve anyway? – holex Nov 04 '14 at 11:11
  • I wanted to have a ViewController that displays properties and actions, different for different selected item, in different tableViews. I setup properties and values, then actions available for that items with selector names. And for actions i may need to display the "Delete" action after all actions. – Svenv Nov 12 '14 at 14:56

1 Answers1

0

NSDictionary is not a ordered container.If you use it,you should not care the order of data.

If you want a ordered dictionary,refer to this link

http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html

Or you may write another NSArray/NSMutableArray to keep the keys order of this NSDictionary.

Or just use NSArray instead of NSDictionary,one array keep keys,another array keep values

Hope this could be helpful

Leo
  • 24,596
  • 11
  • 71
  • 92
  • Thanks, that was useful ... I decided to use 2 dimensional NSArray instead of NSDictionary. This way I am solved the problem of having an "action-name", "action title", "action value". – Svenv Nov 04 '14 at 09:44
  • @slava At the expense of look-up speed. – trojanfoe Nov 04 '14 at 09:57
  • @trojanfoe Possible, but I am using only 6 items. I know that is not an excuse for writing inefficient code but... what else can I do, use plain C arrays ? how? I am trying to set data source for a ViewController with table that displays property-value list and actions ( for selected item ) – Svenv Nov 04 '14 at 10:27
  • @slava You use both. A dictionary to hold key/values of the actual data and an array of the keys to hold the order you want that data to appear in, allowing re-ordering etc. – trojanfoe Nov 04 '14 at 11:18