简体   繁体   English

从NSDictionary对象的NSArray中,如何获取唯一的NSArray

[英]From NSArray of NSDictionary objects, how to get an NSArray of uniques

I'm writing an app that deals with Albums of pictures. 我正在编写一个处理图片相册的应用。

I have the following JSON document which lists Albums. 我有以下列出了相册的JSON文档。

{
  "Albums": [
    {
      "id": 1234,
      "Title": "My Birthday 2008",
      "Category": {
        "id": 0,
        "Name": "Birthdays"
      }
    },
    {
      "id": 5678,
      "Title": "Disneyland Trip 2010"
      "Category": {
        "id": 1,
        "Name": "Trips"
      },
    }
  ]
}

As you can see, Albums reside in Categories. 如您所见,专辑位于“类别”中。

I've used NSJSONSerialization class to bring this into an NSDictionary object which contains an NSArray of NSDictionaries (per the "schema" of the above JSON). 我已经使用NSJSONSerialization类将其带入一个NSDictionary对象,该对象包含一个NSDictionaries的NSArray(根据上述JSON的“模式”)。

From this, I want to generate an NSArray of unique Categories NSDictionary objects. 由此,我想生成一个具有唯一Categories NSDictionary对象的NSArray。 Something like: 就像是:

{
  "Categories": [
    {
      "id": 0,
      "Name" : "Birthdays"
    },
    {
      "id": 1,
      "Name" : "Trips"
    },
  ]
}

Scouring stackoverflow, I've found some things that seem like good building blocks 搜寻stackoverflow,我发现了一些看起来不错的构建基块

categories = [albums valueForKeyPath:@"@distinctUnionOfObjects.Category.Name"];

but that obviously only generates an NSArray of NSStrings. 但这显然只会生成NSStrings的NSArray。 What I want is it to be an NSArray of NSDictionaries which has two objects in it with keys "id" and "Name". 我想要的是一个NSDictionaries的NSArray,其中有两个对象,它们的键为“ id”和“ Name”。 I can't seem to put it all together to get what I want, without the poorman/brute method of nested if loops. 如果没有嵌套if循环的可怜/粗俗的方法,我似乎无法将所有内容组合在一起以获得我想要的东西。 I guess I'm looking for some utility method for the NSDictionary or NSArray classes that can help me do this faster. 我想我正在寻找一些NSDictionary或NSArray类的实用方法,可以帮助我更快地完成此操作。

Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM