Pregunta

I have a NSMutableDictionary *categoryTableArray; which has data in this format

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

On selecting some items for deletion I will get a id list in NSMutableArray *deletedArray; which is in this format

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

I need an efficient way to delete the object corresponding to the id in delete list which is in my categoryTableArray. My current solution is to modify my Delete list to have a Generation label as well and then delete, I was wondering if there is any other efficient way without modifying the Delete list array.

¿Fue útil?

Solución

Here's some test code I wrote for you. Enjoy :)

- (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]];
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top