iOS - Add NSDictinary key values into an NSArray sequencialy

我是研究僧i 提交于 2020-01-05 21:09:32

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/30749219/ios-add-nsdictinary-key-values-into-an-nsarray-sequencialy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!