如何将 NSDictionarys 的 NSArray 转换为 NSDictionarys 的嵌套 NSArray

发布于 2024-09-28 19:23:58 字数 798 浏览 0 评论 0原文

如果我有一个带有多个字典对象(每个字典都是一个人)的 NSArray (或可变数组),如下所示:

[
  { forename: chris, surname: smith, age: 22 }
  { forename: paul, surname: smith, age: 24 }
  { forename: dave, surname: jones, age: 21 }
  { forename: alan, surname: jones, age: 26 }
  { forename: cecil, surname: reeves, age: 32 }
  { forename: pablo, surname: sanchez, age: 42 }
]

我如何将其分成四个数组的数组,按姓氏(并在该名字内)排序,如下所示:

[
    [ 
      { forename: alan, surname: jones, age: 26 }
      { forename: dave, surname: jones, age: 21 }
    ]
    [  
      { forename: cecil, surname: reeves, age: 32 }
    ]
    [
      { forename: pablo, surname: sanchez, age: 42 }
    ]
    [ 
      { forename: chris, surname: smith, age: 22 }
      { forename: paul, surname: smith, age: 24 }
    ]
]

If I have an NSArray (or mutable array) with several dictionary objects (each dictionary is a Person) like this:

[
  { forename: chris, surname: smith, age: 22 }
  { forename: paul, surname: smith, age: 24 }
  { forename: dave, surname: jones, age: 21 }
  { forename: alan, surname: jones, age: 26 }
  { forename: cecil, surname: reeves, age: 32 }
  { forename: pablo, surname: sanchez, age: 42 }
]

How would I separate it into an array of four arrays, sorted by surname (and within that forename), like so:

[
    [ 
      { forename: alan, surname: jones, age: 26 }
      { forename: dave, surname: jones, age: 21 }
    ]
    [  
      { forename: cecil, surname: reeves, age: 32 }
    ]
    [
      { forename: pablo, surname: sanchez, age: 42 }
    ]
    [ 
      { forename: chris, surname: smith, age: 22 }
      { forename: paul, surname: smith, age: 24 }
    ]
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-10-05 19:23:58

您可以使用键值编码和谓词做一些非常简洁的事情...

//retrieve an array of the distinct surnames
NSArray * distinctSurnames = [peopleArray valueForKeyPath:@"@distinctUnionOfObjects.surname"];
//at this point, if you need the surnames in a specific order, you can do so here.

NSMutableArray * groupedPeople = [NSMutableArray array];

//for each of the surnames...
for (NSString * surname in distinctSurnames) {
  //create a predicate to only find people with this surname
  NSPredicate * filter = [NSPredicate predicateWithFormat:@"surname = %@", surname];
  //retrieve only the people with this surname and add them to the final array
  [groupedPeople addObject:[peopleArray filteredArrayUsingPredicate:filter]];
}

这适用于 Person 对象(这会更好)或字典,只要字典有 >@"surname" 键或 Person 对象有一个 -(NSString *)surname 方法。

You can do some pretty neat stuff using key-value coding and predicates...

//retrieve an array of the distinct surnames
NSArray * distinctSurnames = [peopleArray valueForKeyPath:@"@distinctUnionOfObjects.surname"];
//at this point, if you need the surnames in a specific order, you can do so here.

NSMutableArray * groupedPeople = [NSMutableArray array];

//for each of the surnames...
for (NSString * surname in distinctSurnames) {
  //create a predicate to only find people with this surname
  NSPredicate * filter = [NSPredicate predicateWithFormat:@"surname = %@", surname];
  //retrieve only the people with this surname and add them to the final array
  [groupedPeople addObject:[peopleArray filteredArrayUsingPredicate:filter]];
}

This will work for either a Person object (which would be better) or dictionaries, as long as the dictionaries have a @"surname" key or the Person object has a -(NSString *)surname method.

只是我以为 2024-10-05 19:23:58

您应该使用专门的 Person 类,而不是 NSDictionary。


您可以首先将其分组为字典数组的字典,例如,

NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (Person* person in peopleArray) {
    NSString* surname = person.surname;
    NSMutableArray* surnameArray = [result objectForKey:surname];
    if (surnameArray == nil) {
        [result setObject:[NSMutableArray arrayWithObject:person]
                   forKey:surname]; 
    } else {
        [surnameArray addObject:person];
    }
}
return result;

如果您确实需要一个数组,则可以使用[result allValues]

如果需要排序数组,则需要手动排序:

NSArray* sortedSurnames = [[result allKeys] sortedArrayUsingSelector:@selector(compare:)];
return [result objectsForKeys:sortedSurnames notFoundMarker:[NSNull null]];

You should use a specialized Person class, instead of an NSDictionary.


You could first group it into Dictionary of Arrays of Dictionaries, e.g.

NSMutableDictionary* result = [NSMutableDictionary dictionary];
for (Person* person in peopleArray) {
    NSString* surname = person.surname;
    NSMutableArray* surnameArray = [result objectForKey:surname];
    if (surnameArray == nil) {
        [result setObject:[NSMutableArray arrayWithObject:person]
                   forKey:surname]; 
    } else {
        [surnameArray addObject:person];
    }
}
return result;

You could use [result allValues] if you really need an Array.

If you need a sorted array, you need to sort it manually:

NSArray* sortedSurnames = [[result allKeys] sortedArrayUsingSelector:@selector(compare:)];
return [result objectsForKeys:sortedSurnames notFoundMarker:[NSNull null]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文