Ra1nWarden
Ra1nWarden

Reputation: 1220

NSPredicate Creation

how shall I create an NSPredicate to that test whether a NSString start with some pattern, say "AAA"? Thank you very much! I tried to read Apple's reference but could not understand it.

Upvotes: 1

Views: 77

Answers (2)

juniperi
juniperi

Reputation: 3773

Like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@", @"AAA"];

And if you want to find all matching strings from array:

for(NSString *n in [myArray filteredArrayUsingPredicate:predicate]) {
    NSLog(@"%@", n);
}

And answer to your comment about 'c' and 'd' characters etc:

SELF is in this case NSString object

BEGINSWITH[] explanation not needed, I think.

"String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively" (quote from Apple's Predicates Programming Guide)

Upvotes: 4

user7388
user7388

Reputation: 1741

You can try this one:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", Your_text];

Upvotes: 0

Related Questions