You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
I'm sure there are other use cases for this, but here's my current one. I have a list where one item is selected and I have to remember which one it is. So I stash the object id in NSUserDefaults, and every time I fetch my results, I need to know the indexPath of my selected item so I can tell the table it's selected.
For now, I've implemented this extension to do it for me. One method to find it by id, and more general purpose one that can take any NSPredicate to find the first object that matches some condition(s).
In my case, I only have one section, so I'm only searching a single section. Someone else might want to search all sections.
import RealmResultsController
extensionRealmResultsController{func indexPathOf(sectionIndex:Int, id:AnyObject)->NSIndexPath?{returnindexPathOf(sectionIndex, predicate:NSPredicate(format:"id == %@", argumentArray:[id]))}func indexPathOf(sectionIndex:Int, predicate:NSPredicate)->NSIndexPath?{
for row in 0..<numberOfObjectsAt(sectionIndex){letindexPath=NSIndexPath(forRow: row, inSection: sectionIndex)letobject=objectAt(indexPath)
if predicate.evaluateWithObject(object as?AnyObject){return indexPath
}}returnnil}}
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm sure there are other use cases for this, but here's my current one. I have a list where one item is selected and I have to remember which one it is. So I stash the object id in NSUserDefaults, and every time I fetch my results, I need to know the indexPath of my selected item so I can tell the table it's selected.
For now, I've implemented this extension to do it for me. One method to find it by id, and more general purpose one that can take any NSPredicate to find the first object that matches some condition(s).
In my case, I only have one section, so I'm only searching a single section. Someone else might want to search all sections.
The text was updated successfully, but these errors were encountered: