领域日期查询

iKK*_*iKK 7 realm tableview ios

在Xcode6.3下的我的RealmSwift(0.92.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()    
}
Run Code Online (Sandbox Code Playgroud)

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if let cnt = RLM_array?.objects(NameEntry).count {
        return Int(cnt)
    }
    else {
        return 0
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个问题:我怎样才能找到日期条目的对象数量,比如15.06.2014的日期?(即日期查询高于RealmSwift-Object中的特定日期 - 这是如何工作的?).或者换句话说,上面的方法将如何找到具有所需日期范围的对象数?

将所有Realm-Objects成功填充到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
    }
}        
Run Code Online (Sandbox Code Playgroud)

第二个问题:我如何填写表格只查看具有特定日期的RealmSwift-对象(例如,仅填充具有高于15.06.2014的日期的对象).或者换句话说,上面的方法如何只填充tableView具有所需日期范围的对象?

小智 17

您可以使用日期查询Realm.

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

使用具有特定NSDate对象的谓词将执行您想要的操作:

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

问题1:对于对象的数量,只需呼叫计数: results.count

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

要创建特定的NSDate对象,请尝试以下答案:如何为特定日期创建NSDate?