本文介绍了使用NSPredicate删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多子词典的下载词典。
如何使用 NSPredicate

I have the folloving dictionary which has many sub dictionaries.How can I remove objects where isChanged = 1 from parent dictionary using NSPredicate?

{
    "0_496447097042228" =     {
        cellHeight = 437;
        isChanged = 1;
    };
    "100000019882803_193629104095337" =     {
        cellHeight = 145;
        isChanged = 0;
    };
    "100002140902243_561833243831980" =     {
        cellHeight = 114;
        isChanged = 1;
    };
    "100004324964792_129813607172804" =     {
        cellHeight = 112;
        isChanged = 0;
    };
    "100004324964792_129818217172343" =     {
        cellHeight = 127;
        isChanged = 0;
    };
    "100004324964792_129835247170640" =     {
        cellHeight = 127;
        isChanged = 1;
    };
}


推荐答案

我解决了我的问题以下方式:

I solved my problem in the following way:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isChanged == %d", 1];

NSArray *allObjs = [parentDict.allValues filteredArrayUsingPredicate:predicate];

[allObjs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:0];
    [keys setArray:[parentDict allKeysForObject:obj]];
    [parentDict removeObjectsForKeys:keys];
    [keys release];
}];

这篇关于使用NSPredicate删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:24