I have an Entity "Person" with following attributes:
I created numbers of objects:
(Ben, Black, 18)
(John, Smith, 19)
(Ivan, Borzov, 18)
(Den, Balan, 20)
(Kent, Broman, 20)
How to receive array or any other way to build a table with only unique ages [18,19,20]
Please provide answer in Swift.
P.S. Of course I can fetch all objects, and search for unique manually, but I hope for better solution)
You can both
returnsDistinctResults properties of NSFetchRequest to get distinct result of ages across all entities.
let fetchRequest = NSFetchRequest(entityName: "YourEntity") fetchRequest.resultType = .DictionaryResultType fetchRequest.propertiesToFetch = ["age"] fetchRequest.returnsDistinctResults = true let result = try! managedObjectContext.executeFetchRequest(fetchRequest)