NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; //查询条件 NSString *str = @:"川菜"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dishClass = %@",str]; [request setPredicate:predicate];//设置查询条件,即查询所有dishClass为川菜的数据 //依照dishCount对查询结果排序 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dishCount" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; NSError *error = nil; //获得一个包含order对象的数组 NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; //遍历取出数据 for (int j = 0;j < mutableFetchResults.count; j++) { Order *order = [mutableFetchResults objectAtIndex:j]; NSString *name = order.dishName; NSString *class = order.dishClass; NSString *count = [NSString stringWithFormat:@"%d",order.dishName]; }
coredata 查询操作的核心代码
最新推荐文章于 2024-08-24 22:42:50 发布