2013-02-23 47 views
0

我有一个实体&三个属性叫做name,version,Company。 我想过滤公司属性的值,如“快餐”。使用NSPredicate如何从核心数据中过滤值

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

但我不能做这个PLZ帮助我。

回答

0

你不必用引号括起来的属性名称:

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

或更好

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

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


(针对您的意见)你应该定义实体范畴有一个一对多的关系:

enter image description here

现在,你可以找到所有具有以下取阅请求的给定类别的菜肴:

NSString *catName = @"Desert"; 
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Dish"]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category.name = %@", catName]; 
[request setPredicate:predicate]; 
+0

你好....马丁..我们可以聊这个问题 – 2013-02-23 14:36:55

+0

@AbhishekRathore:如果你描述的问题我会尽力帮忙。 – 2013-02-23 14:38:27

+0

其实我想根据一个类别获取信息,例如,让我们拿一个餐馆菜单,在这里我想根据管理员准备的物品清单输入的值显示菜单卡上的值。 例如,在甜品类别中插入冰淇淋,将桃子汤插入汤类中。这些项目需要按照选择的类别在菜单中显示。 – 2013-02-23 14:44:20

相关问题