对核心数据实体的NSSets进行排序 - Objective-c [英] Sorting NSSets of a core data entity - Objective-c
问题描述
我想对核心数据NSSet的数据进行排序(我知道我们只能用数组做,但让我解释一下......)。我有一个实体用户与实体配方有很多关系。配方具有属性名称和ID。
我想得到这样的数据:
I would like to sort the data of a core data NSSet (I know we can do it only with arrays but let me explain...). I have an entity user who has a relationship to-many with the entity recipe. A recipe has the attributes name and id. I would like to get the data such that:
NSArray *id = [[user.recipes valueForKey:@"identity"] allObjects];
NSArray *name = [[user.recipes valueForKey:@"name"] allObjects];
如果我在两个数组中取索引1处的对象,它们对应于相同的配方......
if I take the object at index 1 in both arrays, they correspond to the same recipe...
谢谢
推荐答案
您需要先对食谱进行排序:
You need to sort the recipes first:
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
NSArray *sortedRecipes = [[recipes allObjects] sortedArrayUsingDescriptors:sortDescriptors];
然后,您可以从已排序的配方数组中提取属性数组,结果将按排序顺序保留:
you can then extract an array of attributes from the sorted recipes array and the results will remain in sorted order:
NSArray *sortedNames = [sortedRecipes valueForKey:@"name"];
NSArray *sortedIdentities = [sortedRecipes valueForKey:@"identity"];
这篇关于对核心数据实体的NSSets进行排序 - Objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!