Saturday, December 7, 2013

CoreData string search

I have a database field is a name, which has a record is AA1
I want him to find out, the code is written

+ (NSManagedObject *) GetObjectFrom: (NSString *) tablename ForFieldName: (NSString *) fieldname ForValue: (NSString *) value {

AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * managedObjectContext = appDelegate.managedObjectContext;

NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription * entityDescription = [NSEntityDescription
entityForName: tablename
inManagedObjectContext: managedObjectContext];
[Request setEntity: entityDescription];
NSPredicate * pred = [NSPredicate
predicateWithFormat: @ "(% @ =% @)", fieldname, value];
[Request setPredicate: pred];
NSError * error;
NSArray * objects = [managedObjectContext executeFetchRequest: request
error: & error];
[Request release];
if ([objects count]> 0) {
return [objects objectAtIndex: 0];
}
else
return nil;

}

I also tried
NSPredicate * pred = [NSPredicate
predicateWithFormat: @ "(% @ = '% @')", fieldname, value];
Or
NSPredicate * pred = [NSPredicate
predicateWithFormat: @ "(% @ ==% @)", fieldname, value];
NSPredicate * pred = [NSPredicate
predicateWithFormat: @ "(% @ == '% @')", fieldname, value];

Seems to not work, how to write before they can

Reply:
It should not be difficult now, how nobody back ah
Reply:
NSPredicate * pred = [NSPredicate predicateWithFormat: @ "% @ ==% @", fieldname, value];
Reply:
reference to the second floor of the reply:
NSPredicate * pred = [NSPredicate predicateWithFormat: @ "% @ ==% @", fieldname, value];


No use, this one I've tried, but no, there is no data to identify the NSArray

No comments:

Post a Comment