Four ways for making an NSPredicate

I learn by examples. I find that it connects things in my brain that abstract description does not.

   NSSet *set = [[NSSet alloc] initWithArray:@[@(3), @(4), @(5), @(7), @(9), @(15)]];

    NSLog(@"%@", [set filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"SELF == %@", @(3)]]);

    NSLog(@"%@", [set filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"SELF == %@" argumentArray:@[@(3)]]]);

    NSLog(@"%@", [set filteredSetUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {

        return [(NSNumber *)evaluatedObject isEqualToNumber:@(3)];

    }]]);

    NSLog(@"%@", [set filteredSetUsingPredicate:[[NSPredicate predicateWithFormat:@"SELF == $number"] predicateWithSubstitutionVariables:@{@"number" : @(3)}]]);

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s