1

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;
    };
}
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
Shamsiddin Saidov
  • 2,281
  • 4
  • 23
  • 35

3 Answers3

4

As a simple alternative to using NSPredicate, you can use the NSDictionary's built in keysOfEntriesPassingTest: This answer assumes "isChanged" is an NSString and the value 0 or 1 is an NSNumber:

NSSet *theSet = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
    return [obj[@"isChanged"] isEqualToNumber: @1];
}];

The returned set is a list of keys that pass the test. From there, you could remove all that matched with:

[dict removeObjectsForKeys:[theSet allObjects]];

J Shapiro
  • 3,861
  • 1
  • 19
  • 29
  • The code in the block can be simplified to just : return [obj[@"isChanged"] isEqual: @1]; There's no need for the if-else clause. – rdelmar Nov 24 '12 at 16:57
  • You don't need to avoid literals for iOS<=5, because that's backward compatible to iOS4 as you can see here http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html and the block based filtering method that you have used is available from iOS4 onwards - so writing two methods isn't needed. – Abizern Nov 24 '12 at 17:42
  • I was going to leave the verbose answer for clarity, but I think you're right @rdelmar, it's pretty clear even without the clause. – J Shapiro Nov 24 '12 at 17:42
  • @JShapiro Finally - sorry to be picky - but the comparison should really be done with `isEqualToNumber:` It's better to be explicit with object comparisons, even though lower valued NSNumbers are singletons, that is an implementation detail that shouldn't be relied upon. – Abizern Nov 24 '12 at 17:57
  • @Abizern I appreciate you being picky :) Editing answer. – J Shapiro Nov 24 '12 at 18:07
2

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];
}];
Shamsiddin Saidov
  • 2,281
  • 4
  • 23
  • 35
0

when you have array of dictionary than you can remove selected category's data using NSPredicate

here is code

NSString *selectedCategory = @"1";

//filter array by category using predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isChanged == %@", selectedCategory];

NSArray *filteredArray = [yourAry filteredArrayUsingPredicate:predicate];

[yourAry removeObject:[filteredArray objectAtIndex:0]];  

But in your problem data is not in array it is in dictionary

your data should be in this format

(
 {
     cellHeight = 437;
     isChanged = 1;
 },
 {
     cellHeight = 145;
     isChanged = 0;
 },
 {
     cellHeight = 114;
     isChanged = 1;
 }
 )
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74