Search code examples
iosobjective-cnsstringnspredicate

Save the result of setPredicate to NSString


I have created NSPredicate to search the string in my coredata. so in this case does anyone know how to save the result [fetchRequest setPredicate:predicate]; in NSString.

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

Solution

  • Write Your Table Name in @"YOUR_ENTITY_NAME"

    Make sure that your table contains name column

    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