我正在尝试从特定日期输入的CoreData对象中获取值。我在SO和其他网站上发现了一些有点类似的问题,并且四处乱逛,将看起来似乎应该起作用的代码拼凑在一起,但是它并没有为我所知道的数据返回任何值。我是一个相对入门的人,因此很赞赏基本的回答。下面的代码。抱歉,这太过分了,但是我不确定故障在哪里。预先感谢。
@IBAction func plusDayToEdit(_ sender: Any) {
if dayToEdit < 30 {
dayToEdit += 1
dayToEditLabel.text = String(dayToEdit)
previousDayFetchRequest()
} else {
}
func previousDayFetchRequest() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Entry")
var calendar = Calendar.current
calendar.timeZone = NSTimeZone.local
let dateToEdit = calendar.date(byAdding: .day, value: (0 - dayToEdit), to: Date())
let startDate = calendar.startOfDay(for: dateToEdit!)
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate)
let fromPredicate = NSPredicate(format: "entryDate <= %@", startDate as NSDate)
let toPredicate = NSPredicate(format: "entryDate > %@", endDate! as NSDate)
let dateToEditPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
fetchRequest.predicate = dateToEditPredicate