gpt4 book ai didi

ios - 显示核心数据 : If attribute has same name display once

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:41:37 25 4
gpt4 key购买 nike

我查看了许多谓词问题,我已经阅读了文档,但似乎没有什么可以跳出来回答我的问题。

我有一个名为Materials 的核心数据实体,并且我有属性categorysubcategorydescription

我有三个 UITableViewControllers,我想在每个中使用谓词来显示如下:

TableViewController 1:只有类别(不重复类别名称)

选择一个类别并转到 TableViewController 2。

TableViewController 2:显示子类别(不重复子类别名称)

选择一个子类别并转到 TableViewController 3 列出该类别和子类别中的所有项目。

如果不在核心数据模型中使用三个实体,我可以这样做吗?

我已经尝试在我的 fetchedResultsController 方法中使用以下谓词代码但没有用:

Materials * materials = [[Materials alloc]init];//this doesn't feel like it belongs inside a fetchedResultsController

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"category == %@", materials.category];

fetchRequest.predicate = predicate;

这是我第一次尝试以这种方式进行排序和显示,我通常会按照惯例使用关系谓词,但对于一组数据( Material )使用 3 个实体似乎不合逻辑。

最佳答案

对于每个 Material 、类别和子类别,您不需要三个不同的 NSMO。您只需要一个 NSMO,即具有这些属性类别、子类别和描述的 Material 。

在 ViewController 1 中显示类别:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Materials" inManagedObjectContext:self.managedObjectContext];

// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct Categories, only ask for the 'Category' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"Category"]];
fetchRequest.returnsDistinctResults = YES;

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

有了这个,您可以从核心数据中获取所有具有不同类别的 Material NSManagedObjects。

在 ViewController 2 中显示子类别:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Materials" inManagedObjectContext:self.managedObjectContext];


NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF.category == %@", selectedCategoryName];

fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct SubCategory, only ask for the 'SubCategory' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"SubCategory"]];
fetchRequest.returnsDistinctResults = YES;

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

有了这个,您可以从核心数据中获取所有具有不同类别的 Material NSManagedObjects。

要让第三个 Viewcontroller 列出属于选定类别和子类别的所有项目,请使用:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Materials" inManagedObjectContext:self.managedObjectContext];


NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF.category == %@ and SELF.subcategory == %@", selectedCategoryName,selectedSubcatgory];

fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

关于ios - 显示核心数据 : If attribute has same name display once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27538930/

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