NSArray 中的 iOS sdk NSArray 查找和合并

Posted

技术标签:

【中文标题】NSArray 中的 iOS sdk NSArray 查找和合并【英文标题】:iOS sdk NSArray within NSArray find and merge 【发布时间】:2017-02-21 04:43:40 【问题描述】:

我正在尝试在多个 NSArray 中搜索和合并对象。

基本上 NSArray 具有多个 NSArray 和 1 个对象,然后是 NSDictionary。我想检查对象是否已经存在并将其与现有对象合并,那么它将是 NSSArray,它有一个 NSArray 和 1 个对象,然后是多个 NSDictionary。在 Objective-c 中。

(
  (
    "The Essential: Céline Dion",
    
      "album": "The Essential: Céline Dion",
      "artwork": "<UIImage: 0x608000087210>, 512, 506",
      "title": "It's All Coming Back to Me Now"
    
  ),
  (
    "Ah Via Musicom",
    
      "album": "Ah Via Musicom",
      "artwork": "<UIImage: 0x60000008a6e0>, 600, 600",
      "title": "Cliffs of Dover"
    
  ),
  (
    "Tears Roll Down (Greatest Hits 82-92)",
    
      "album": "Tears Roll Down (Greatest Hits 82-92)",
      "artwork": "<UIImage: 0x6080000887a0>, 512, 512",
      "title": "Everybody Wants to Rule the World"
    
  ),
  (
    "No Money - Single",
    
      "album": "No Money - Single",
      "artwork": "<UIImage: 0x6080000845b0>, 600, 600",
      "title": "No Money"
    
  ),
  (
    "Tears Roll Down (Greatest Hits 82-92)",
    
      "album": "Tears Roll Down (Greatest Hits 82-92)",
      "artwork": "<UIImage: 0x60800008d5c0>, 512, 512",
      "title": "Shout"
    
  ),
  (
    "STAB!",
    
      "album": "STAB!",
      "artwork": "<UIImage: 0x60800008da20>, 512, 512",
      "title": "Will You Remember Me?"
    
  ),
  (
    "Life After Sundown",
    
      "album": "Life After Sundown",
      "artwork": "<UIImage: 0x60000008d840>, 600, 600",
      "title": "Werewolves On Wheels"
    
  )
)

到这里

(
  (
    "The Essential: Céline Dion",
    
      "album": "The Essential: Céline Dion",
      "artwork": "<UIImage: 0x608000087210>, 512, 506",
      "title": "It's All Coming Back to Me Now"
    
  ),
  (
    "Ah Via Musicom",
    
      "album": "Ah Via Musicom",
      "artwork": "<UIImage: 0x60000008a6e0>, 600, 600",
      "title": "Cliffs of Dover"
    
  ),
  (
    "Tears Roll Down (Greatest Hits 82-92)",
    
      "album": "Tears Roll Down (Greatest Hits 82-92)",
      "artwork": "<UIImage: 0x6080000887a0>, 512, 512",
      "title": "Everybody Wants to Rule the World"
    ,
    
      "album": "Tears Roll Down (Greatest Hits 82-92)",
      "artwork": "<UIImage: 0x60800008d5c0>, 512, 512",
      "title": "Shout"
    
  ),
  (
    "No Money - Single",
    
      "album": "No Money - Single",
      "artwork": "<UIImage: 0x6080000845b0>, 600, 600",
      "title": "No Money"
    
  ),
  (
    "STAB!",
    
      "album": "STAB!",
      "artwork": "<UIImage: 0x60800008da20>, 512, 512",
      "title": "Will You Remember Me?"
    
  ),
  (
    "Life After Sundown",
    
      "album": "Life After Sundown",
      "artwork": "<UIImage: 0x60000008d840>, 600, 600",
      "title": "Werewolves On Wheels"
    
  )
)

【问题讨论】:

样本不一样。第二个显示 Tears Roll Down (Greatest Hits 82-92) 以我想要的方式合并在一起。 【参考方案1】:

我们将您的原始数组称为mainArray

首先,您从 mainArray 收集唯一名称。

NSMutableArray *uniqueNames = [NSMutableArray array];

for (NSArray *sub in mainArray) 
    NSString *name = [sub firstObject];
    BOOL isDuplicate = [[uniqueNames filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains %@",name]] count] > 0;
    if (!isDuplicate) 
        [uniqueNames addObject:name];
    

之后,循环遍历 uniqueNames 数组并在 mainArray 中搜索重复项并重新构建数组。

NSMutableArray *final = [NSMutableArray array];
for (NSString *name in uniqueNames) 
    NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF contains %@",name];
    NSArray *duplicateArrays = [mainArray filteredArrayUsingPredicate:namePredicate];
    if ([duplicateArrays count] > 1) 
        NSMutableArray *newArray = [NSMutableArray arrayWithObject:name];
        for (NSArray *duplicateArray in duplicateArrays) 
            [newArray addObject:[duplicateArray lastObject]];
        
        [final addObject:newArray];
    
    else
        [final addObject:duplicateArrays];
    

final 数组就是你的结果。

【讨论】:

以上是关于NSArray 中的 iOS sdk NSArray 查找和合并的主要内容,如果未能解决你的问题,请参考以下文章

NSPredicate的使用

oc70--NSArray1

iPhone SDK 中的 NSMutableArray、NSArray、NSString 内存泄漏

在 for 循环中添加到 NSArray 会导致重复?

在视图初始化之前调用视图控制器中的方法 - iPhone iOS 5 SDK

NSArray