iOS - Add NSDictinary key values into an NSArray s

2019-08-08 15:00发布

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条回答
Fickle 薄情
2楼-- · 2019-08-08 15:29

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

查看更多
你好瞎i
3楼-- · 2019-08-08 15:35

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

查看更多
登录 后发表回答