Using NSPredicate how to filter value from core data using 1 entity

不羁岁月 提交于 2019-12-11 15:07:15

问题


I have one entity & three Attribute called as item, rate, category. i want to filter value from category attribute.

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

but i cant do this plz help me.


回答1:


Replace your initialization of predicate from the code below and try

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


来源:https://stackoverflow.com/questions/15109741/using-nspredicate-how-to-filter-value-from-core-data-using-1-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!