NSPredicate and nspredicate

Source: Internet
Author: User

NSPredicate and nspredicate

Cocoa provides a class named NSPredicate, which is used to specify filter conditions and describe the query method using NSPredicate. The principle is similar to querying in a database. The NSPredicate class can be used in database-style APIs, commonly used for Core Data and Spotlight. This article describes how to use a certain category, such as garage (garage class) and car (car), for example only. The creation code is not listed. Understanding the usage of predicate is the key.


Create Predicate.

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'Herbie'"];

Calculate Predicate

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"engine.horsepowe > 150"];    NSArray *cars = [garage cars];    for (Car *car in [garage cars]) {        if ([predicate evaluateWithObject:car]) {            NSLog(@"%@",car.name);        }    }

Traverse the array to retrieve objects that meet the conditions.


Array Filter

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"engine.horsepowe > 150"];    NSArray *results;    results = [cars filteredArrayUsingPredicate:predicate];

Suppose you have a variable array and you need to remove all items that do not belong to the array. Use the following methods:

    NSMutableArray *carsCopy = [cars mutableCopy];    [carsCopy filterUsingPredicate:predicate];



Flexible Predicate construction

There are two ways,

First format specifier

    NSPredicate *predicate1, *predicate2, *predicate3;        predicate1 = [NSPredicate predicateWithFormat:@"engine.horsepowe > %d",50];        predicate2 = [NSPredicate predicateWithFormat:@"name == %@",@"Herbie"];        predicate3 = [NSPredicate predicateWithFormat:@"%k == %@", @"name", @"Herbie"];

The second method is to put the variable name in a string, which is similar to the environment variable.

This predicate is equivalent to predicate3 and predicate2 in the first one.

    NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];        NSDictionary *varDict;    varDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Herbie",@"NAME", nil];        NSPredicate *predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];


This predicate is equivalent to predicate1 in the first one.

    predicateTemplate = [NSPredicate predicateWithFormat:@"engine.horsepower > $POWER"];        varDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:150], @"POWER", nil];        predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];



Predicate Operator

Common operators supporting C language ==><>=! = <>& |! AND OR NOT

    predicate = [NSPredicate predicateWithFormat:@"(engine.horsepower > 50) AND (engine.horsepower < 200)"];    results = [cars filteredArrayUsingPredicate:predicate];

Array Operators

    predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN{50,200}"];

Equivalent

    NSArray *betweens = [NSArray arrayWithObjects:[NSNumber numberWithInt:50],[NSNumber numberWithInt:200],nil];    predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BEWEEN %@",betweens];

SELF keyword response predicate calculation object

    NSArray *names1 = [NSArray arrayWithObjects:@"Herbie",@"Badger",@"Judge",@"Elvis", nil];    NSArray *names2 = [NSArray arrayWithObjects:@"Judge",@"Paper Car",@"Badger",@"Phoenix", nil];        predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", names1];    results = [names2 filteredArrayUsingPredicate:predicate];    NSLog(@"%@", results);

The answer is:

(
Judge,

Badger

)


String Operators

BEGINSWITH check whether a string starts with another string

ENDSWITH checks whether a string ends with another string


CONTAINS checks whether a string is inside another string
For example, if "name BEGINSWITH 'bad'" is used to match a Badger, It is case sensitive. If it matches a BADGer, it cannot match.
To ignore the rules such as uppercase and lowercase letters and heavy notes, you can add [cd] "name BEGINSWITH [cd] 'herb'" to match Herbie. Generally, it is better to add [cd ].

LIKE Operator
"Name LIKE '* er *'" will match any name containing er, equivalent to CONTAINS "name LIKE '??? Er * '"will match Paper Car but will not match Badger. er is limited to three characters. If you are keen on regular expressions, you can use the MATCHES operator, assign it a regular expression.

Reprinted please indicate the original:Http://blog.csdn.net/marvindev







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.