views:

37

answers:

1

Hello!

I am searching for a solution on how to format NSPredicate to search correct word in a string of text. Currently I am using this code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(content CONTAINS[c] %@)", word];

but it returns wrong resuls if the word is short, for example: if the word is 'ail' it will return all strings with words which include this 3 letters.. But I don't need such abstract search, so how to search words 'beginswith' my word in a string?

+1  A: 

Simply use BEGINSWITH instead of CONTAINS.

Edit

If you need to search in every word of a string, there is a technique which was presented in one of the talks in WWDC 2010. The basic idea is to create a separate entity Word which contains a single word and a reference of the containing object (the entity you're searching). You then do the search on the Word entity and return the objects which are related to the found words.

The query would then look something like this (provided you have a many-to-one relationship between your entity and Word: "ANY words BEGINSWITH[c] %@"

This would mean that you have to setup the words when creating your objects though.

frenetisch applaudierend
Thank you for your time, but I am speaking about a string, not a word. If I will use beginswith it will return all strings which begins with my word, but I need to return strings which contains words, begins with my word...
azia
So what you need is something like "any word in content begins with XX"?
frenetisch applaudierend
Yes, something like this. But it should return whole string. The thing is I need to find in ny Core Data recipes with one of ingredients by recipe title. For example if I search apple I need to find recipes with title 'Fuji Apples Tarte Tatin'.
azia
I edited my answer to give you an example on how you could do it. This is from a WWDC 2010 Core Data talk, so if you're a registered developer you can have a look at the videos.
frenetisch applaudierend
@Azia I would recommend using "CONTAINS" in that case
Dave DeLong
It is strange for me, but if I use just "content beginswith[c] %@" - it also works correct. So your first answer was also correct. Thanks!
azia