在
Xcode6.3下的我的Realm
Swift(0.92.3)中,我将如何
@H_502_2@// 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查找对象的数量(即当前所有对象),如下所示:
@H_502_2@func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { if let cnt = RLM_array?.objects(NameEntry).count { return Int(cnt) } else { return 0 } }第一个问题:我怎样才能找到日期条目的对象数量,比如15.06.2014的日期? (即,在RealmSwift-Object的特定日期之上的日期查询 – 这是如何工作的?).或者换句话说,上面的方法将如何找到具有所需日期范围的对象数?
将所有Realm-Objects成功填充到tableView中如下所示:
@H_502_2@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 } }第二个问题:我如何填写表格仅查看具有特定日期的RealmSwift-对象(例如,仅填充具有高于15.06.2014的日期的对象).或者换句话说,上面的方法如何只填充tableView具有所需日期范围的对象?
解决方法
您可以使用日期查询Realm.
如果您想在日期之后获取对象,请使用大于(>),对于之前的日期,请使用less-than(<). 使用具有特定NSDate对象的谓词将执行您想要的操作:
@H_502_2@let realm = Realm() let predicate = NSPredicate(format: "date > %@",specificNSDate) let results = realm.objects(NameEntry).filter(predicate)问题1:对于对象的数量,只需调用count:results.count
问题2:结果是特定的NSDate之后的NameEntrys数组,在indexPath获取对象.例如,让nameEntry = results [indexPath.row]
要创建特定的NSDate对象,请尝试以下答案:How do I create an NSDate for a specific date?