私は毎月の経費を追跡するアプリを持っています。私は経費エンティティ月現在の月の経費を追跡する属性が作成されています。私はここに示すように、毎月の費用をテーブルビューで表示します。ユーザーは左スイッチができ、費用はSwiftで空のオブジェクトを扱うにはどうすればいいですか?
@IBAction func backMonthButtonPressed(sender: AnyObject) {
print("Back Button Pressed")
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentMonth, options: [])!
if checkMonth(currentMonth) {
updateFetch()
setMonth()
} else {
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentMonth, options: [])!
}
tableView.reloadData()
}
@IBAction func nextMonthButtonPressed(sender: AnyObject) {
print("Next Button Pressed")
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentMonth, options: [])!
if checkMonth(currentMonth) {
updateFetch()
setMonth()
} else {
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentMonth, options: [])!
}
tableView.reloadData()
}
func checkMonth(month : NSDate) -> Bool {
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let context = app.managedObjectContext //scratch pad
let fetchRequest = NSFetchRequest(entityName: "Expense")
fetchRequest.predicate = NSPredicate(format: "month == %@", month.MonthYearDateFormatter())
let count = context.countForFetchRequest(fetchRequest, error: nil)
if count > 0 {
print("There are expenses for this month \(month.MonthYearDateFormatter()). Show expenses")
return true
} else {
print("There are no expenses for this month \(month.MonthYearDateFormatter()). Do Nothing")
return false
}
}
次または先月中にある右の場合にのみ、私の問題は、ユーザーが戻って6月に費用を作成し、作成しなかったとは考えにくいシナリオでは、これです8月の費用。どのように私は8月に彼/彼女の経費をそれをスキップせずに見たままにさせることができますか?何か案は?
すべての費用を一度に取得し、月単位で並べ替えて配列に格納します。次に、配列をナビゲートします – Paulw11