gpt4 book ai didi

ios - 如何使用 Swift 处理空对象?

转载 作者:行者123 更新时间:2023-11-28 16:10:13 25 4
gpt4 key购买 nike

我有一个跟踪每月开支的应用程序。我有一个 Expense 实体,它有一个 month 属性,用于跟踪创建当前月份的费用。然后我会在表格 View 中显示每个月的费用,如此处所示。用户只有在下个月或上个月有费用时才可以左右切换

@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 月份没有创建费用。我怎样才能让用户在不跳过它的情况下仍然看到他/她在八月份的费用。有任何想法吗?

最佳答案

我在阐述之前做了一些优化:

@IBAction func backMonthButtonPressed(sender: AnyObject) {
self.processMonth(step: -1)
}

@IBAction func nextMonthButtonPressed(sender: AnyObject) {
self.processMonth(step: 1)
}

// a method uses almost the same code for both cases, so it was merged
func processMonth(step: Int) {
let direction = (step < 1 ? "Back" : "Next")
print("\(direction) Button Pressed")

currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: step, toDate: currentMonth, options: [])!

//if checkMonth(currentMonth) {
// I wouldn't test this because it locks you out from seeing empty month.
updateFetch()
setMonth()
//}

tableView.reloadData()
}

你所问的问题的答案:

如果您的 UITableView 的数据源设置正确,您应该能够度过空白的月份

// changed return type from `Bool` to `void` as I suggested in the method not to test empty month, as it could be useless

func checkMonth(month : NSDate) {
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")

} else {
print("There are no expenses for this month \(month.MonthYearDateFormatter()). Do Nothing")
// here you can make some additional actions, like seeting the empty table with "no expenses this month"
}
}

无论如何,正如 @Paulw11 指出的那样,如果您的数据源不会耗尽大小,您宁愿从 viewDidLoad/viewDidAppear 的数据模型中获取数据,然后每月呈现根据 currentMonth 变量(关于用户当前看到的月份)。

因此,您只能在 Controller 加载时以及每次用户更改当前月份 View 时调用 setMonth() 方法。

关于ios - 如何使用 Swift 处理空对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com