问题
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