Reputation: 19
i have the following code
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Word== %@", randomWord];
rows = [[courseArray filteredArrayUsingPredicate:predicate]retain];
I am using this in a search bar. I have two questions - how can i make the NSPredicate so it takes every word that begins with the characters typed rather than having a value equal another? e.g I have "Men, Many,Morons, Now,Never" in the array, and when i type "M" i want "Men,Many,Morons" to be added. Furthermore, how can i predicate more than 1 value? so it compares between two formats?
thanks...
Upvotes: 0
Views: 380
Reputation: 120
You need create all of the predicate which you want to compare and append all predicate to array. after that you can use NSCompoundPredicate
for or
,and
condition.
like that
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"is_neutered = 0"],
[NSCompoundPredicate orPredicateWithSubPredicates:feedDays]]];
Upvotes: 1
Reputation: 28688
The NSPredicate documentation is a good place to start.
The predicate to check if the string begins with another is [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@"]
And as far as checking two predicates, you're looking for NSCompoundPredicate
.
Upvotes: 1
Reputation: 1741
Try
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@", randomWord];
If you want to search the whole word you can try CONTAINS
..
Upvotes: 1
Reputation: 3940
Look for the 'Prefix' part under the String Constants, Variables, and Wildcards section.
Upvotes: 0