本文介绍了iOS - 将 NSDictinary 键值添加到 NSArray 序列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSDictionary 和一个 NSArray:

I have a NSDictionary and a NSArray:

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

这些是我的NSDictionary的关键和价值:

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"]};

现在我想要的是将我的 self.dataDic 的所有 key 值依次插入到 self.dataDicKey 中.这意味着它看起来像这样:

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];

我已经尝试过:

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.

非常感谢.

推荐答案

如果您试图在 NSArraykeys 作为 objects> 你应该试试这个方法

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]];

要记住的一件事是 NSDictionary allKeys 方法只返回键,而不管它们被保存的顺序.作为它的 KVP

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

这会将您的 NSDictionary 键复制到 NSArray

This will copy your NSDictionary keys into NSArray

这篇关于iOS - 将 NSDictinary 键值添加到 NSArray 序列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:45