0

我有一个实体和三个属性,分别称为名称、版本、公司。我想从公司属性中过滤值,例如“快餐”。

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"('company' 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];

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

4

1 回答 1

0

您无需将属性名称括在引号中:

[NSPredicate predicateWithFormat:@"company CONTAINS[c] 'Fast Foood'"];

或更好

[NSPredicate predicateWithFormat:@"company CONTAINS[c] %@", @"Fast Foood"];

顺便说一句:您的谓词搜索“快餐”,而不是“快餐”。


(回应您的评论)您应该使用一对多关系定义实体类别菜肴:

在此处输入图像描述

现在,您可以使用以下获取请求找到给定类别的所有菜肴:

NSString *catName = @"Desert";
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Dish"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category.name = %@", catName];
[request setPredicate:predicate];
于 2013-02-23T14:32:29.113 回答