2015-05-17 26 views
5

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

目前的tableView查找對象(即目前的所有對象)的數量一樣如下:

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的特定日期之上的日期查詢 - 這是如何工作的?)。換句話說,上面的方法將如何找到具有所需日期範圍的對象的數量?

所有領域對象成的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對象僅填充日期再次爲15.06.2014以上的對象)。或者換句話說,上面的方法如何才能在tableView中填充所需的日期範圍?

回答

13

您可以使用日期查詢Realm。

如果要在日期後獲取對象,請在日期前使用大於(>)的小於(<)的日期。

使用具有特定的NSDate對象謂詞會做你想要什麼:

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

問題1:對於對象的數目,只需要調用數:results.count

問題2:results是一個數組在specificNSDate之後的NameEntrys,在indexPath處獲取對象。例如,let nameEntry = results[indexPath.row]

要創建特定的NSDate對象,請嘗試此答案:How do I create an NSDate for a specific date?