- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我查看了许多谓词问题,我已经阅读了文档,但似乎没有什么可以跳出来回答我的问题。
我有一个名为Materials 的核心数据实体,并且我有属性category、subcategory 和description。
我有三个 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/
我是一名优秀的程序员,十分优秀!