问题
Here is an example.
class Person: Object {
dynamic var id
dynamic var name
}
// does this work?
let sortedPeople = realm.objects(Person).sorted("id")
let Dave = realm.objects(Person).filter("id=5")
// at what index does Dave reside in sortedPeople?
The reason I need to find out about this is coz I have a UITableView that is set to the sortedPeople, but I need to store the last visible row viewed. The sortedPeople array changes frequently. So, if I can find out the index in the sortedPeople of where the person is, I can create a NSIndexPath and scroll to that row.
回答1:
you can use indexOf:
method to find index of particular object,
try this
let sortedPeople = realm.objects(Person).sorted("id")
let Dave = realm.objects(Person).filter("id=5")
//this will return optional Int?
let indexOfDave = sortedPeople.indexOf(Dave)
来源:https://stackoverflow.com/questions/34849910/how-to-get-the-index-in-results-of-a-certain-realm-object