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

Launch iPhone simulator without Xcode.

How to Make REST api call in Objective-C.

id vs NSObject