使用NSPredicate如何使用1个实体来过滤从核心数据值(Using NSPredicate h

2019-10-17 18:18发布

我有一个实体法三个属性称为项目,速率类别。 我想过滤从类别属性值。

    if (managedObjectContext==nil) {
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        managedObjectContext = appDelegate.managedObjectContext;

    }

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Restaurantmenu" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"('category' contains[c] 'Fast Foood')"];
[request setPredicate:predicate];
NSLog(@"Pre %@",predicate);
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    // Set self's events array to the mutable array, then clean up.
    [self setDataarr:mutableFetchResults];

但我不能这样做plz帮助我。

Answer 1:

从下面的代码替换您的谓词的初始化和尝试

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category CONTAINS[c] %@", @"Fast Foood"]; 


文章来源: Using NSPredicate how to filter value from core data using 1 entity