2017-02-21 3 views
0

複数のNSArrayでオブジェクトを検索してマージしようとしています。iOS sdk NSArray内のNSArrayを検索してマージする

NSArrayは基本的に1つのオブジェクトを持つ複数のNSArrayを持ち、次にNSDictionaryを持ちます。オブジェクトが既に存在するかどうかを確認して既存のオブジェクトとマージしてください。そうすれば、1つのオブジェクトを持つNSArrayと複数のNSDictionaryがあるNSSArrayになります。 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" 
    } 
) 
) 
+1

サンプルは同じではありません。 2番目の曲は、Tears Roll Down(Greatest Hits 82-92)が好きなように合体したことを示しています。 – ipodtouchdude

答えて

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配列が結果です。

関連する問題