Realm date-query
You can query Realm with dates.
If you want to get objects after a date, use greater-than (>), for dates before, use less-than (<).
Using a predicate with a specific NSDate object will do what you want:
let realm = Realm()
let predicate = NSPredicate(format: "date > %@", specificNSDate)
let results = realm.objects(NameEntry).filter(predicate)
Question 1: For the number of objects, just call count: results.count
Question 2: results
is an array of NameEntrys after specificNSDate
, get the object at indexPath. Example, let nameEntry = results[indexPath.row]
For creating a specific NSDate object, try this answer: How do I create an NSDate for a specific date?
![iKK](https://i.stack.imgur.com/odxpv.jpg?s=256&g=1)
iKK
Updated on June 11, 2022Comments
-
iKK about 2 years
In my RealmSwift (0.92.3) under Xcode6.3, how would I
// the Realm Object Definition import RealmSwift class NameEntry: Object { dynamic var player = "" dynamic var gameCompleted = false dynamic var nrOfFinishedGames = 0 dynamic var date = NSDate() }
The current tableView finds the number of objects (i.e. currently all objects) like follows:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let cnt = RLM_array?.objects(NameEntry).count { return Int(cnt) } else { return 0 } }
First question: How would I find the number of objects that have a date-entry after, let's say, the date of 15.06.2014 ?? (i.e. date-query above a particular date from a RealmSwift-Object - how does that work ?). Or in other words, how would the above method find the number of objects with the needed date-range ??
The successful filling of all Realm-Objects into a tableView looks as follows:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("NameCell") as! PlayersCustomTableViewCell if let arry = RLM_array { let entry = arry.objects(NameEntry)[indexPath.row] as NameEntry cell.playerLabel.text = entry.player cell.accessoryType = entry.gameCompleted ? .None : .None return cell } else { cell.textLabel!.text = "" cell.accessoryType = .None return cell } }
Second question: How would I fill into the tableView only the RealmSwift-Objects that have a particular date (i.e. for example filling only the objects that have again the date above 15.06.2014). Or in other words, how would the above method only fill into the tableView the objects with the needed date-range ??