Thor Larson
Thor Larson

Reputation: 43

How to find an index of an attribute in core data

I found how to find the index of an element in an array:

let arr = ["a","b","c"]

let indexOfA = arr.indexOf("a") //0
let indexOfB = arr.indexOf("b") //1
let indexOfC = arr.indexOf("c") //2

Is there a way to apply this to Core Data to find the index of a certain attribute? Lets say I have an entity called Events. In Events there is 3 attributes: eventName, eventLocation, eventDate. If there are many events saved is there a way to enter, for example, one of the names saved and return the index that it is located in similar to the way above?

Upvotes: 0

Views: 640

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

Not directly in Core Data, because Core Data doesn't sort the data, so there is no first, second, third, etc, there are just a bunch of instances. When you fetch data from Core Data via NSFetchRequest, you can tell it how to sort the results, using the sortDescriptors property. The fetch request gives you an array of results, sorted according to those descriptors. You could then find the index of an event name in that array.

Upvotes: 1

Related Questions