我有一个tableView,如果这个entity-attribute之一等于一个字符串,它应该只显示coreData-entity的对象。
有一个名为购买的实体,其中一个属性是名为 position
的字符串在我的tableViewController代码中是:
@property (strong) NSMutableArray *buy;
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Buy"];
self.buy= [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
现在我尝试只显示购买的对象,其中位置字符串等于“a”。但我不知道如何编写if条件。
感谢您事先得到答案。
答案 0 :(得分:2)
执行以下操作: -
NSString *str="a";
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"position == %@",str];
因此,viewDidAppear函数变为: -
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSString *str="a";
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"position == %@",str];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Buy"];
[fetchRequest setPredicate : predicate ];
self.buy= [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}