iphone - using NSPredicate contains to find characters inside a coredata entity's field -


I am trying to find all the customers in which there is a certain sequence of letters I have the same functionality as the range of NSString Wish insensitive in the case except for stressing. My method is:

  - (NSArray *) db_search: (NSString *) table where: (NSString *) field contains: (NSString *) Source field with value: (NSString *) Sortfield {NSFetchRequest * request = [[[NSFetchRequest alloc] init] autorelease]; NSEntityDescription * entity = [NSEntityDescription EntityForName: Managed Object Contact in Table: Reference]; If (field of! = Zero) {NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "(% @ in [c]% @)", field, value]; [Request to certification: count]; } [Request Setty: Unit]; If (sorted! = Zero) {nsort descriptor * sorting descriptor = [[[NSSSTRDcriptwriter alloc] Init Witke: [Self ExtractShorted: Sortfield] Ascending: [Volunteer: sortfield]] Autorier]; NSArray * Sort Descriptor = [[[NSArray alloc] initWithObjects: SortDescriptors, Zero] AutoWrecks]; [Request Associate Circuit: Sort Descriptor]; } NSError * error; Return [reference executeFetchRequest: request error: & amp; Error]; }   

I'm calling it with these values:

  NSArray * result = [self db_search: @ "customer" Where: @ "fullname" In: @ "Matt" with Sourcefield: zero];   

Rather than acquiring all Mats, Matthews, etc., when I try to print the results, it freezes it. I debug it and we did not get an empty NSARR. I print NSArray for the console and I do not get 0 element. I

I have tried to do a dump of DB for the console and it contains all the right things in it.

= UPDATE ====================================== ========================

I am using% K and I get a weird runtime error Has been:

  if (searchResults1! = Zero) {NSLog (@ "% match:% i", [search result1 number]); } And {nslog (@ "null was"); }   

On NSLog (@ "% match: line. This is a bad EXC error. Then searchResults1 is not a null, but when I try to read the count then it crashes? When I debug, SearchResults1 is actually an NSArray but there is nothing like this in it.

It seems that your problem is your definition:

  NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "(% @ in [c]% @)", field key, value]   

when you type fieldKey = @ "fullname" and value = @ If you pass through "matte" , then this bill is going to be equal to it: [NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "('fullname' includes [ Matt]] "];

Do you see the problem?" As a raw string, and not as the field name. This is because you have < Code>% @ modifier. When NSPredicate arrives at all, it says "Ah! The price that will be replaced here is a constant. "

Instead of using % @ , instead of using ," Aha! The value that is substituted here will be an identifier. "% K . This is a special modifier for only a prediction, and this means that in the string as an identifier (actually" keypath Means "option", which means that it will make your prediction:

  NSPredicate * Predicate = [NSPredicate predicateWithFormat: @ "(full name [c] 'mat' ) "];   

That's what you are looking for.

Comments