ios - 显示核心数据 : If attribute has same name display once
<p><p>我查看了许多谓词问题,我阅读了文档,但似乎没有什么可以作为我的问题的答案。 </p>
<p>我有一个名为 <em>Materials</em> 的核心数据实体,我有属性 <em>category</em>、<em>subcategory</em> 和 <em>description</em>。 </p>
<p>我有三个 <code>UITableViewControllers</code> 并且在每个中我想使用一个谓词来显示如下:</p>
<p><strong>TableViewController 1:只有类别(类别名称不重复)</strong></p>
<p>选择一个类别并转到 TableViewController 2。</p>
<p><strong>TableViewController 2:显示子类别(不重复子类别名称)</strong></p>
<p>选择一个子类别并转到 TableViewController 3 列出该类别和子类别中的所有项目。</p>
<p>如果不在核心数据模型中使用三个实体,我可以这样做吗? </p>
<p>我尝试在我的 <code>fetchedResultsController</code> 方法中使用以下谓词代码,但成功了:</p>
<pre><code>Materials * materials = [init];//this doesn't feel like it belongs inside a fetchedResultsController
NSPredicate * predicate = ;
fetchRequest.predicate = predicate;
</code></pre>
<p>这是我第一次尝试以这种方式排序和显示,我通常会按照惯例使用关系谓词,但为一组数据( Material )设置 3 个实体似乎不合逻辑。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您不需要为每个 Material 、类别和子类别设置三个不同的 NSMO。您只需要一个 NSMO,它是具有这些属性类别、子类别和描述的 Material 。</p>
<p>在 ViewController 1 中显示类别:</p>
<pre><code>NSFetchRequest *fetchRequest = ;
NSEntityDescription *entity = ;
// 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 = objectForKey:@"Category"]];
fetchRequest.returnsDistinctResults = YES;
// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = ;
NSLog (@"names: %@",dictionaries);
</code></pre>
<p>通过它,您可以从核心数据中获取具有不同类别的所有 MaterialNSManagedObject。 </p>
<p>用于在 ViewController 2 中显示子类别:</p>
<pre><code>NSFetchRequest *fetchRequest = ;
NSEntityDescription *entity = ;
NSPredicate * predicate = ;
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 = objectForKey:@"SubCategory"]];
fetchRequest.returnsDistinctResults = YES;
// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = ;
NSLog (@"names: %@",dictionaries);
</code></pre>
<p>通过它,您可以从核心数据中获取具有不同类别的所有 MaterialNSManagedObject。 </p>
<p>让第三个 Viewcontroller 列出属于 Selected 类别和子类别的所有项目:</p>
<pre><code>NSFetchRequest *fetchRequest = ;
NSEntityDescription *entity = ;
NSPredicate * predicate = ;
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 = ;
NSLog (@"names: %@",dictionaries);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 显示核心数据 : If attribute has same name display once,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27538930/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27538930/
</a>
</p>
页:
[1]