Predicates

A predicate is a useful object that filters an array. It’s a bit like having a select with a simple where clause in SQL.

If you have array in this format


NSMutableArray *yourAry;
(
        {
        category = classic;
        quote = "Frankly, my dear, I don't give a damn.";
        source = "Gone With the Wind";
    },
        {
        category = classic;
        quote = "Here's looking at you, kid.";
        source = Casablanca;
    }
{
        category = modern;
        quote = "I solemnly swear that I am up to no good.";
        source = "Harry Potter and the Prisoner of Azkaban";
    },
        {
        category = modern;
        quote = "You like pain? Try wearing a corset.";
        source = "Pirates of the Caribbean";
    }
)


And you want to search all the array which category = classic then we can use NSPredicate.

NSString *selectedCategory = @"classic";

   //filter array by category using predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", selectedCategory];
    NSArray *filteredArray = [self.movieQuotes filteredArrayUsingPredicate:predicate];


Comments

Post a Comment

Popular posts from this blog

Hacker Rank problem solution

How to Make REST api call in Objective-C.

Building and Running Python Scripts with Xcode