领域日期查询 [英] Realm date-query

查看:65
本文介绍了领域日期查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

第一个问题:我将如何查找在2014年6月15日之后有日期输入的对象的数量? (即,来自RealmSwift对象的特定日期上方的日期查询-这是如何工作的?).换句话说,上述方法将如何找到所需日期范围内的对象数量?

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

将所有Realm-Object成功填充到tableView中的样子如下:

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

第二个问题:如何将只填充具有特定日期的RealmSwift对象填充到表中(例如,仅填充日期再次高于2014年6月15日的对象).换句话说,上述方法将如何仅将具有所需日期范围的对象填充到tableView中?

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

推荐答案

您可以使用日期查询Realm.

You can query Realm with dates.

如果要在日期之后获取对象,请使用大于号(>),对于之前的日期,请使用小于号(<).

If you want to get objects after a date, use greater-than (>), for dates before, use less-than (<).

将谓词与特定的NSDate对象一起使用将满足您的要求:

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)

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

Question 1: For the number of objects, just call count: results.count

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

Question 2: results is an array of NameEntrys after specificNSDate, get the object at indexPath. Example, let nameEntry = results[indexPath.row]

要创建特定的NSDate对象,请尝试以下答案:

For creating a specific NSDate object, try this answer: How do I create an NSDate for a specific date?

这篇关于领域日期查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆