O'Reilly logo

Core Data in Swift by Marcus S. Zarra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NSSortDescriptor

NSSortDescriptor has been around longer than Core Data, and it’s still quite useful for ordering data. As mentioned previously, data that comes from a to-many relationship is unordered by default, and it’s up to us to order it. So, if we wanted to retrieve all the recipes and sort them by their name property in alphabetical order, we’d require another step as part of the fetch.

 let​ fetch = ​NSFetchRequest​(entityName: ​"Recipe"​)
 fetch.sortDescriptors = [​NSSortDescriptor​(key: ​"name"​, ​as​cending: ​true​)]

In this example, we are retrieving all the Recipe entities by creating an NSFetchRequest with the NSEntityDescription set to our entity and no predicate. However, in addition to fetching the Recipe entities, we want ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required