iOS - Add NSDictinary key values into an NSArray sequencialy

338 views Asked by At

I have a NSDictionary and a NSArray:

self.dataDic = [[[NSDictionary alloc] init] autorelease];
self.dataDicKey = [[[NSArray alloc] init] autorelease];

These are the key and value of my NSDictionary:

    self.dataDic =
    @{@"Ring Cloud" :@[@"Contact Cloud", @"Image Cloud", @"Data Cloud"],
      @"Ring Tools" :@[@"Sticker Market", @"Live Poll", @"Mail"],
      @"Ring Apps"  :@[@"Studio", @"Player"]};

Now what I want is to insert all the key value of my self.dataDic into self.dataDicKey sequentially. That means it looks like this:

self.dataDicKey = [[[NSArray alloc] initWithObjects:@"Ring Cloud", @"Ring Tools", @"Ring Apps",nil] autorelease];

I have tried with this:

self.imageDicKey = [[[self.imageDic allKeys] reverseObjectEnumerator] allObjects];

But it's not actually sorted the values. If any one have any suggestion please share it with me.

Thanks a lot in advance.

2

There are 2 answers

0
Syed Ali Salman On BEST ANSWER

If you are trying to get the keys as objects in your NSArray you should try this method

self.DataDicKey = [[NSArray alloc]initWithArray:[self.imageDic allKeys]];

One thing to remember that NSDictionary allKeys method returns only the keys regardless of order they were saved. as its KVP

This will copy your NSDictionary keys into NSArray

0
ozd On

NSDictionary keys are unsorted - maybe you could save the keys in array before you enter the data to the dictionary