将setPredicate的结果保存到NSString

时间:2018-03-08 09:38:25

标签: ios objective-c nsstring nspredicate

我创建了NSPredicate来搜索我的coredata中的字符串。 所以在这种情况下,有人知道如何在NSString中保存结果[fetchRequest setPredicate:predicate];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",
                                  mytext];
[fetchRequest setPredicate:predicate];

2 个答案:

答案 0 :(得分:1)

@"YOUR_ENTITY_NAME"

中写下您的表名

确保您的表格包含name

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"YOUR_ENTITY_NAME"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", mytext]];
NSMutableArray *arrResult = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSLog(@"%@", arrResult); //output

答案 1 :(得分:0)

我认为这会对你有帮助

NSArray *fetchedObjects;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"your entity"  inManagedObjectContext: context];
[fetch setEntity:entityDescription];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", mytext]];
NSError * error = nil;
fetchedObjects = [context executeFetchRequest:fetch error:&error];

if([fetchedObjects count] == 1)
NSLog(@"%@",[fetchedObjects objectAtIndex:0]);