Here is an example:
Match the Regular Expression of 9-15 strings consisting of letters/numbers:
NSString * regex = @ "^ [A-Za-z0-9] {9, 15} $ ";
NSPredicate * pred = [NSPredicate predicateWithFormat: @ "self matches % @", regex];
BOOL isMatch = [pred evaluateWithObject: txtfldPhoneNumber. text];
Cocoa uses NSPredicate to describe the query method. The principle is similar to querying in a database.
Use BETWEEN, IN, BEGINWITH, ENDWITH, CONTAINS, and LIKE predicates to construct NSPredicate. If necessary, use SELF to directly match yourself.
[Cpp]
// Basic query
NSPredicate * predicate;
Predicate = [NSPredicate predicateWithFormat: @ "name = 'herbie '"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@ "% s", (match )? "YES": "NO ");
// Compare loops in the whole cars
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * cars = [garage cars];
For (Car * car in [garage cars]) {
If ([predicate evaluateWithObject: car]) {
NSLog (@ "% @", car. name );
}
}
// Output complete information
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * results;
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// The predicate containing the variable
NSPredicate * predicateTemplate = [NSPredicate predicateWithFormat: @ "name = $ NAME"];
NSDictionary * varDict;
VarDict = [NSDictionary dictionaryWithObjectsAndKeys:
@ "Herbie", @ "NAME", nil];
Predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
NSLog (@ "SNORGLE: % @", predicate );
Match = [predicate evaluateWithObject: car];
NSLog (@ "% s", (match )? "YES": "NO ");
// Note that $ VARIABLE cannot be used as the path name because its value represents the value.
// Supports common operators in C Language
Predicate = [NSPredicate predicateWithFormat:
@ "(Engine. horsepower> 50) AND (engine. horsepower <200)"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "oop % @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name <'Newton '"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
// Powerful array Operators
Predicate = [NSPredicate predicateWithFormat:
@ "Engine. horsepower BETWEEN {50,200}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
NSArray * betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower BETWEEN % @", betweens];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
PredicateTemplate = [NSPredicate predicateWithFormat: @ "engine. horsepower BETWEEN $ POWERS"];
VarDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @ "POWERS", nil];
Predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// IN Operator
Predicate = [NSPredicate predicateWithFormat: @ "name IN {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
Predicate = [NSPredicate predicateWithFormat: @ "SELF. name IN {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
Names = [cars valueForKey: @ "name"];
Predicate = [NSPredicate predicateWithFormat: @ "self in {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [names filteredArrayUsingPredicate: predicate]; // The SELF range is limited here.
NSLog (@ "% @", results );
// BEGINSWITH, ENDSWITH, CONTAINS
// Additional symbols, [c], [d], [cd], and c indicate case-insensitive, d indicates non-distinguishing pronunciation characters, and cd indicates no distinguishing between words.
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'bad'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'hb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH [cd] 'herb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// LIKE operator (wildcard)
Predicate = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '* er *'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '??? Er * '"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// Basic query
NSPredicate * predicate;
Predicate = [NSPredicate predicateWithFormat: @ "name = 'herbie '"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@ "% s", (match )? "YES": "NO ");
// Compare loops in the whole cars
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * cars = [garage cars];
For (Car * car in [garage cars]) {
If ([predicate evaluateWithObject: car]) {
NSLog (@ "% @", car. name );
}
}
// Output complete information
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * results;
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// The predicate containing the variable
NSPredicate * predicateTemplate = [NSPredicate predicateWithFormat: @ "name = $ NAME"];
NSDictionary * varDict;
VarDict = [NSDictionary dictionaryWithObjectsAndKeys:
@ "Herbie", @ "NAME", nil];
Predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
NSLog (@ "SNORGLE: % @", predicate );
Match = [predicate evaluateWithObject: car];
NSLog (@ "% s", (match )? "YES": "NO ");
// Note that $ VARIABLE cannot be used as the path name because its value represents the value.
// Supports common operators in C Language
Predicate = [NSPredicate predicateWithFormat:
@ "(Engine. horsepower> 50) AND (engine. horsepower <200)"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "oop % @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name <'Newton '"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
// Powerful array Operators
Predicate = [NSPredicate predicateWithFormat:
@ "Engine. horsepower BETWEEN {50,200}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
NSArray * betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower BETWEEN % @", betweens];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
PredicateTemplate = [NSPredicate predicateWithFormat: @ "engine. horsepower BETWEEN $ POWERS"];
VarDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @ "POWERS", nil];
Predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// IN Operator
Predicate = [NSPredicate predicateWithFormat: @ "name IN {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
Predicate = [NSPredicate predicateWithFormat: @ "SELF. name IN {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", [results valueForKey: @ "name"]);
Names = [cars valueForKey: @ "name"];
Predicate = [NSPredicate predicateWithFormat: @ "self in {'herbie ', 'snugs', 'badger', 'flap'}"];
Results = [names filteredArrayUsingPredicate: predicate]; // The SELF range is limited here.
NSLog (@ "% @", results );
// BEGINSWITH, ENDSWITH, CONTAINS
// Additional symbols, [c], [d], [cd], and c indicate case-insensitive, d indicates non-distinguishing pronunciation characters, and cd indicates no distinguishing between words.
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'bad'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'hb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH [cd] 'herb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// LIKE operator (wildcard)
Predicate = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '* er *'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
Predicate = [NSPredicate predicateWithFormat: @ "name LIKE [cd] '??? Er * '"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );