将 setPredicate 的结果保存到 NSString

Save the result of setPredicate to NSString

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

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

我想这对你有帮助

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]);

@"YOUR_ENTITY_NAME"

中写下你的Table名字

确保您的 table 包含 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