我有两个实体:

议程事件

议程日期

AgendaDates与AgendaEvents具有一对多的关系。

我试图存储在一个临时数组中(var myTempEvents = [AgendaEvent]())
在AgendaDates内部具有日期的所有AgendaEvent都等于定义的日期(让myDate = Date())

到目前为止,我有这个:

 var myEventDate = [String]()
 var myTempEvents = [AgendaEvent]()
 var myEvents = [AgendaEvent]()
 var myDate = Date()

 func getEventDates() {
    for event in myEvents {
        for date in (event.agendaDates as? Set<AgendaDate>)! {
            let eventDates = date.agendaDates
            eventDate = eventDates
            formatter.dateFormat = "dd MM yyyy"
            let eventDateString = formatter.string(from: eventDate)
            myEventDate.append(eventDateString)

        }
    }
}


我现在需要做的是检查AgendaEvents是否具有等于myDate的日期,如果是的话,我需要将该事件添加到myTempEvents。

这应该在此函数内发生:

func configureCell(cell: CalendarAgendaCell, indexPath: IndexPath) {
    for dates in calendar.selectedDates {
        for dateOfEvent in myEventDate {
            formatter.dateFormat = "dd MM yyyy"
            let dateToCompare = formatter.string(from: dates)
            if dateOfEvent == dateToCompare {
                let myEvent = myTempEvents[indexPath.row]
                cell.configureCell(agendaEvent: myEvent)

            } else {
                //empty the tempArray

            }
        }
    }
}


这个函数由cellForRowAt函数调用tableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Dequeue Cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "AgendaCell", for: indexPath) as! AgendaCell
    //Fetch model object to display
    configureCell(cell: cell, indexPath: indexPath)
    return cell
}


我不太擅长核心数据(仍在学习中),因此我们将不胜感激。

谢谢!

更新--------- 14/09/17

正如@Simo所建议的那样,我已经编辑了我的代码,如下所示:

    func agendaEventsWithDate(date: Date) -> NSFetchRequest<NSFetchRequestResult>
{
    // create a fetch request that will retrieve all the AgendaEvents.
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "AgendaEvent")

    // set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date.
    fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.agendaDates == %@", date as CVarArg)

    return fetchRequest
}

 let myTempEvents = try?context.fetch(agendaEventsWithDate(date: Date()))


上下文在哪里:

let ad = UIApplication.shared.delegate as! AppDelegate


让context = ad.persistentContainer.viewContext

但是我收到此错误:

歧义使用“提取”

谢谢!

最佳答案

有一种更简单的方法可以实现此目的,而不必自己进行比较。您应该阅读将NSFetchRequest与谓词一起使用。

您可以获取存储在Core Data中的所有AgendaEvent,然后对其进行过滤,以便仅剩下包含与指定日期匹配的AgendaDate的事件。

获取请求(包装在一个不错的提供程序函数中)可能看起来像:

func agendaEventsWithDate(date: Date) -> NSFetchRequest
{
    // create a fetch request that will retrieve all the AgendaEvents.
    let fetchRequest = NSFetchRequest(entityName: "AgendaEvent")

    // set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date.
    fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.date == %@", date)

    return fetchRequest
}


显然,这假定您的AgendaEvent实体与AgendaDate的关系称为agendaDates,并且您的AgendaDate实体具有称为date的属性。

然后,当您执行获取请求时,将返回您正在寻找的项目的数组。无需进行所有手动比较。

let myTempEvents = try? managedObjectContext?.executeFetchRequest(self.agendaEventsWithDate(someDate))

08-26 03:20