2017-09-14 2 views
1

H. 나는 Agendadate라는 엔티티와 AgendaEvent라는 엔티티가 있습니다. AgendaEvent에는 AgendaDate (agendaDates)와의 많은 관계가 있습니다.Swift coreData - 형식 날짜와 술어에서 사용

내 AgendaDate에 개체 날짜 (날짜 유형)가 있습니다.

내가 지금 같은 술어를 사용하고 있습니다 : YYYY MM은 HH dd는

"DD MM 일"대신 "의 :

fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg) 

내가 형식으로 시도하고이를 하시려면 mm를 : ss "

두 개의 술어를 시간없이 비교해야합니까? 가능합니까?

UPDATE ----- 여기 내 기능을 사용하면 제안으로 업데이트됩니다 :

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

    // set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date. 
    let cal = Calendar.current 
    let startOfDay = cal.startOfDay(for: eventDate) 
    let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)! 
    print(startOfDay) 
    print(endOfDay) 
    // fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.agendaDates == %@", date as CVarArg) 
    fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@)[email protected] > 0", 
           startOfDay as NSDate, endOfDay as NSDate) 

    return fetchRequest 
} 

을 여기에 셀을 구성하고 선택한 날짜의 같은 날짜가 바로 이벤트를 취해야 기능의를 :

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

      } else { 
       // the array is empty 

      } 
     } 
    } 
} 
+0

핵심 데이터의 날짜에 hh : mm : ss가 있고 술어에서 비교하는 동안 제거하겠습니까? –

+0

@SandeepBhandari 예. 핵심 데이터에는 시간과 함께 저장됩니다. 내가 그것을 비교하기 위해 술어에서 사용할 때 나는 시간이 없으면 날짜를 원합니다. – Marco

+0

@SandeepBhandari :) 감사합니다! – Marco

답변

0

해당 용도로 사용자 지정 문자열 형식을 사용하지 마십시오. 주어진 날짜와 같은 날짜에 날짜가있는 Agendadate 개체와 관련된 모든 항목을 가져 오려고합니다. 그래서

먼저 그 일의 시작과 끝을 계산 :

let predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@)[email protected] > 0", 
          startOfDay as NSDate, endOfDay as NSDate) 

그것은 모든 관련 개체에 대한 $a.dates >= startDate AND $a.dates < endDate을 테스트하고있는 경우는 true 산출 :

let date = Date() 

let cal = Calendar.current 
let startOfDay = cal.startOfDay(for: date) 
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)! 

그런 다음이 하위 쿼리를 사용 적어도 하나는 일치 조건 입니다.

+0

답변 해 주셔서 감사합니다. 아마 나는 정말로 이해하지 못했다. 내가 그렇게하려고하는 것은 특정 agendaDate가있는 모든 AgendaEvent 배열에 배열을 추가하는 것입니다. 그래서 기본적으로 나는 날짜를 선택한다. 날짜가 agendaDate에있는 날짜와 일치하면 그 AgendaEvent를 배열에 저장하고 싶습니다. 어쩌면 내가 뭔가 잘못하고 있지만 모든 AgendaEvent 걸리는 것보다 솔루션을 추가 할 경우. 내 질문을 편집하여 내 코드를 볼 수 있습니다. 고마워요 – Marco

+0

@ 마르코 : 내 제안 된 술어가 그렇게해야합니다. 나는 당신의 코드에서 술어 할당이 주석 처리되었다는 것을 알아 차렸다 :'// fetchRequest.predicate = ...', 그래서 모든 객체가 페치된다. –

+0

죄송합니다 .... 오타. 나는 오래된 것을 주석 처리했다. 코드에서 당신이 트리거됩니다. – Marco