领域date查询

在我的RealmSwift(0.92.3)下的Xcode6.3,我将如何

// the Realm Object Definition import RealmSwift class NameEntry: Object { dynamic var player = "" dynamic var gameCompleted = false dynamic var nrOfFinishedGames = 0 dynamic var date = NSDate() } 

当前的tableView查找对象的数量(即当前所有对象)如下所示:

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let cnt = RLM_array?.objects(NameEntry).count { return Int(cnt) } else { return 0 } } 

第一个问题:我怎么find有一个dateinput对象的数量,比方说,date15.06.2014? (即从RealmSwift-Object的特定date之上的date – 查询 – 这是如何工作的?)。 或换句话说,上面的方法将如何find具有所需date范围的对象的数量?

所有Realm对象成功填充到tableView如下所示:

 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 } } 

第二个问题:如何在tableView中填充只有RealmSwift-Objects的date(例如只填写date在15.06.2014之后的对象)。 或者换句话说,上面的方法怎么会只填充到tableView中具有所需date范围的对象?

您可以使用date查询Realm。

如果要在date之后获取对象,请在date前使用大于(>)的小于(<)的date。

使用一个特定的NSDate对象的谓词将做你想要的:

 let realm = Realm() let predicate = NSPredicate(format: "date > %@", specificNSDate) let results = realm.objects(NameEntry).filter(predicate) 

问题1:对于对象的数量,只需调用count: results.count

问题2: results是在specificNSDate的NSDate之后的一个NameEntrys数组,获取indexPath处的对象。 例如, let nameEntry = results[indexPath.row]

为了创build一个特定的NSDate对象,试试这个答案: 如何创build一个特定date的NSDate?