簡體   English   中英

從NSMutableDictionary中刪除多個值

[英]Deleting multiple values from NSMutableDictionary

我有一個NSMutableDictionary *categoryTableArray; 具有這種格式的數據

MY_CATEGORY : {
    "Generation 2" =     (
                {
            id = 34;
            name = Footwear;
        }
    );
    "Generation 3" =     (
                {
            id = 53;
            name = Party;
        }
    );
    "Generation 5" =     (
                {
            id = 72;
            name = hash1;
        },
                {
            id = 86;
            name = uhgyututyutguhgjhbj;
        }
    );
}

選擇一些要刪除的項目后,我將在NSMutableArray *deletedArray;獲得一個ID列表NSMutableArray *deletedArray; 這是這種格式

Delete List:(
        {
        id = 72;
    },
        {
        id = 53;
    }
)

我需要一種有效的方法來刪除與我的categoryTableArray中的刪除列表中的id對應的對象。 我當前的解決方案是將Delete列表修改為也具有Generation標簽,然后刪除,我想知道是否還有其他有效的方法可以不修改Delete list數組。

這是我為您編寫的一些測試代碼。 請享用 :)

- (void)filterCategories
{
    NSMutableDictionary *categoryTableArray = [@{@"Generation 2": @[@{@"id": @(34), @"name": @"Footwear"}],
                                                 @"Generation 3": @[@{@"id": @(53), @"name": @"Party"}],
                                                 @"Generation 5": @[@{@"id": @(72), @"name": @"hash1"},
                                                                    @{@"id": @(86), @"name": @"uhgyututyutguhgjhbj"}]} mutableCopy];
    NSArray *deleteList = @[@{@"id": @(72)},
                            @{@"id": @(53)}];

    NSLog(@"Before deletion:\n%@", categoryTableArray);
    [self deletIDs:deleteList fromCategories:categoryTableArray];
    NSLog(@"After deletion:\n%@", categoryTableArray);
}

- (void)deletIDs:(NSArray *)deleteList fromCategories:(NSMutableDictionary *)categories
{
    // Convert the array of dictionaries into just an array of IDs.
    NSArray *ids = [deleteList valueForKeyPath:@"id"];
    for (NSString *key in [categories allKeys])
    {
        categories[key] = [categories[key] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT id IN %@", ids]];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM