Just My Life & My Work

[iOS] 排序Core Data Entity

想要排序Core Data Entity如何做到?我們只要針對一個attribute就可做遞增或遞減的排序。

這幾行code即可辦到~比如針對happy這個attribute。

    NSSortDescriptor *sortByHappy = [[NSSortDescriptor alloc] initWithKey:@"happy" ascending:YES];
    NSArray *sortDescriptors = @[sortByHappy];
    // 針對想要排序的Entity Array
    self.happyArr = [[[HMCoreDataManager sharedManager] allHappys] sortedArrayUsingDescriptors:sortDescriptors];

之所以為array型態,表示可以有多個attribute來做多重排序。

參考:What is the best way to sort a Core Data Entity?How to establish secondary NSSortDescriptor sort key?

隨意留個言吧:)~

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料

標籤雲