0

When Loading all key's value of NSDictionary into NSArray class. its incompletely and jumbling (randomly) loading key value of dictionary into NSArray class

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
            //do sth
        colorNameList = [[ColorModelClass colorListNames]allKeys];
    }
    return self;
}

enter image description here

Issue is: i expect all key load from dictionary into array in sequentially not randomly with out missing any key's

kiran
  • 4,285
  • 7
  • 53
  • 98
  • 1
    NSDictionary keys aren't ordered. Even in the moment that you create your dictionary, your keys aren't being stored in the order that you added them. More details here: http://stackoverflow.com/a/22123188/4370893 – VitorMM Apr 06 '17 at 11:46
  • And here: http://stackoverflow.com/a/2496484/4370893 – VitorMM Apr 06 '17 at 11:47
  • refer http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html – Krish Apr 06 '17 at 12:30

1 Answers1

1

I think if you need to sort your dictionary and then store it in your array, it may worked,

Try to sort your Dictionary by doing bellow code:

keyArray = [dicWebColors allKeys];
keyArray = [keyArray sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

Then proceed to store your data into the array.

Thanks

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • First get the keys, then sort the array. You can't sort a dictionary. – Willeke Apr 06 '17 at 15:49
  • "sort your dictionary and then store it in your array" is the other way around. – Willeke Apr 07 '17 at 00:37
  • @Willeke you can't sort your dictionary without array, and if you read the question carefully then perhaps you understand y did i say that. – Abhishek Mitra Apr 07 '17 at 02:23
  • `keyArray = [dicWebColors allKeys];` puts the keys in an array. `sortedArrayUsingSelector` sorts the array. You can't "sort your dictionary". Read your answer carefully. – Willeke Apr 07 '17 at 03:37
  • @Willeke Do you have any better answer ?? Just post it down, let us help with your understanding. Thank you. – Abhishek Mitra Apr 07 '17 at 06:06
  • Instead of "sort your dictionary and then store it in your array" say "store the keys in an array and then sort the array". – Willeke Apr 07 '17 at 10:33