gpt4 book ai didi

ios - 如何在NSManagedContext中知道NSManagedObject的类型

转载 作者:行者123 更新时间:2023-12-01 18:18:45 25 4
gpt4 key购买 nike

我正在使用NSFetchedResultsControllerCore Data获取数据,并在UITableView中显示它们。我的NSFetchedResultsController看起来像这样:

- (NSFetchedResultsController *) fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PlanPreparation"
inManagedObjectContext:_context];
[fetchRequest setEntity:entity];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"plan_id == %@", _planCycleID];
[fetchRequest setPredicate:pred];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];

fetchRequest.sortDescriptors = sortDescriptors;

_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;

// but if there is no results I am making another fetch by another type of NSManagedObject
if (_fetchedResultsController.fetchedObjects.count == 0)
{
NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:@"Preparation"
inManagedObjectContext:_context];
[fetchRequest1 setEntity:entity1];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ismer == %@", [NSNumber numberWithInt:1]];
[fetchRequest1 setPredicate:pred];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];

fetchRequest1.sortDescriptors = sortDescriptors;

_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest1 managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;

//return _fetchedResultsController;
}

return _fetchedResultsController;
}

如果第一次尝试获取内容后我没有重新获取东西,则需要设置另一个获取内容。在 -cellForRowAtIndexPath中,我需要显示数据取决于 NSManagedObjectObject的类型:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([[self.fetchedResultsController objectAtIndexPath:indexPath] isKindOfClass:[PlanPreparation2 class]])
{
PlanPreparation2 *planDrug = (PlanPreparation2 *)[self.fetchedResultsController objectAtIndexPath:indexPath];
CDPreparation *prep = (CDPreparation *)[[PTDataManager sharedManager]getDrugByID:planDrug.guid];
cell.textLabel.text = [NSString stringWithFormat:@"%@", prep.name];
}
else
{
CDPreparation *drug = (CDPreparation *)[self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@", drug.name];
}

return cell;
}

但是日志记录表明 -isKindOfClass:在这里不起作用,我根本没有引号。

题:

如何找出 NSManagedObjectObject的类型?

最佳答案

如图所示,在该实体的数据模型检查器中更改名称和类

数据模型检查器中的名称和类字段必须与实体表的名称相同。

然后你可以比较像

if([(NSManagedObject *)[self.fetchedResultsController objectAtIndexPath:indexPath] isKindOfClass:[Category class]])

因为在我的情况下,“类别”是类,所以您可以在上面的[PlanPreparation2类]中选择自己的类

如果仍然遇到问题,请在下面评论

关于ios - 如何在NSManagedContext中知道NSManagedObject的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com