user765099
user765099

Reputation: 19

nspredicate iphone question

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

Answers (4)

jayesh kanzariya
jayesh kanzariya

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

Joshua Weinberg
Joshua Weinberg

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

Alex
Alex

Reputation: 1741

Try NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@", randomWord];

If you want to search the whole word you can try CONTAINS..

Upvotes: 1

InsertWittyName
InsertWittyName

Reputation: 3940

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html

Look for the 'Prefix' part under the String Constants, Variables, and Wildcards section.

Upvotes: 0

Related Questions