Sorting NSSets of a core data entity - Objective-c

前端 未结 2 1765
感动是毒
感动是毒 2020-12-31 08:57

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 th

2条回答
  •  无人及你
    2020-12-31 09:16

    If you want them to be in the same order, then you need to sort them before extracting the values. Example:

    NSArray * sortedRecipes = [user.recipes sortedArrayUsingDescriptors:arrayOfSortDescriptors];
    NSArray * identities = [sortedRecipes valueForKey:@"identity"];
    NSArray * names = [sortedRecipes valueForKey:@"name"];
    

    EDIT

    My apologies. I just realized this is an iPhone question, and NSSet doesn't have sortedArrayUsingDescriptors: on the iPhone. However, it's trivial to work around:

    NSArray * recipes = [user.recipes allObjects];
    NSArray * sortedRecipes = [recipes sortedArrayUsingDescriptors:arrayOfSortDescriptors];
    ....
    

提交回复
热议问题