【问题标题】:Using NSPredicate how to filter value from core data using 1 entity使用 NSPredicate 如何使用 1 个实体从核心数据中过滤值
【发布时间】:2013-02-13 02:54:47
【问题描述】:

我有一个实体和三个属性,分别称为 item、rate、category。我想从类别属性中过滤值。

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

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

【问题讨论】:

  • 问题是……? (另请注意,'category' 在谓词中应为 category(不带单引号))
  • 这是您之前的问题stackoverflow.com/questions/15041568/… 的几乎完全相同的副本(您接受了答案)。

标签: iphone ios core-data nsfetchrequest


【解决方案1】:

用下面的代码替换你的谓词初始化并尝试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多