如何检索超过 1 个月的 EKEvent?
How do I retrieve an EKEvent that is greater than 1 month old?
使用此代码:
func sampleCase() {
var dateConstructor = DateComponents()
dateConstructor.year = 2016
dateConstructor.month = 12
dateConstructor.day = 15
let calendar: Calendar = Calendar.current
let workCalendar = CalendarUtilities.calendar("WorkSchedule", using: eventStore)
let referenceDate = calendar.date(from: dateConstructor)!
let startDate = referenceDate
let endDate = referenceDate.addingDays(1) // midnight
let range = DateRange(start: startDate, andEnd: endDate)
let predicate = eventStore.predicateForEvents(withStart: range.startDate, end:range.endDate, calendars:[workCalendar])
let events = eventStore.events(matching: predicate)
print("Found: \(events.count) events")
print("used:\(startDate)")
print("\(endDate)")
}
我只得到上个月事件的正确结果...(这些示例使用具有事件的日期并且应该 return 非零值。)
authorization was granted...
Found: 1 events
used:2016-12-15 05:00:00 +0000
2016-12-16 05:00:00 +0000
提前返回会导致 0 个事件。
我也尝试使用枚举得到类似的结果:
eventStore.enumerateEvents(matching: predicate) { (result, stop) in
counter += 1
}
print("found \(counter) items using iterator")
used:2016-11-11 05:00:00 +0000
2016-11-12 05:00:00 +0000
found 0 items using iterator
是否可以检索 2014 年的事件?我在使用日历应用程序(macOS 和 iOS)时看到它们。
事件存储应该 return 只要您不使范围太大(有一个限制,我必须去查找),只要您给它的谓词匹配。在您的示例中,您只告诉它在 1 天的范围内进行搜索。您还将其缩小到一个特定的日历(您可以传递 nil 来搜索所有日历)。如果你想要 2014 年的事件,那么给它一个 2014 年的范围。
我认为发生的事情是我的设备上的设置在 iOS 升级过程中发生了变化(我不知道是如何发生的)。在 Settings/Calendar 中,您可以指定事件的同步方式。我发现它被设置为“1个月”。我现在能够检索超过 1 个月的事件。
使用此代码:
func sampleCase() {
var dateConstructor = DateComponents()
dateConstructor.year = 2016
dateConstructor.month = 12
dateConstructor.day = 15
let calendar: Calendar = Calendar.current
let workCalendar = CalendarUtilities.calendar("WorkSchedule", using: eventStore)
let referenceDate = calendar.date(from: dateConstructor)!
let startDate = referenceDate
let endDate = referenceDate.addingDays(1) // midnight
let range = DateRange(start: startDate, andEnd: endDate)
let predicate = eventStore.predicateForEvents(withStart: range.startDate, end:range.endDate, calendars:[workCalendar])
let events = eventStore.events(matching: predicate)
print("Found: \(events.count) events")
print("used:\(startDate)")
print("\(endDate)")
}
我只得到上个月事件的正确结果...(这些示例使用具有事件的日期并且应该 return 非零值。)
authorization was granted...
Found: 1 events
used:2016-12-15 05:00:00 +0000
2016-12-16 05:00:00 +0000
提前返回会导致 0 个事件。 我也尝试使用枚举得到类似的结果:
eventStore.enumerateEvents(matching: predicate) { (result, stop) in
counter += 1
}
print("found \(counter) items using iterator")
used:2016-11-11 05:00:00 +0000
2016-11-12 05:00:00 +0000
found 0 items using iterator
是否可以检索 2014 年的事件?我在使用日历应用程序(macOS 和 iOS)时看到它们。
事件存储应该 return 只要您不使范围太大(有一个限制,我必须去查找),只要您给它的谓词匹配。在您的示例中,您只告诉它在 1 天的范围内进行搜索。您还将其缩小到一个特定的日历(您可以传递 nil 来搜索所有日历)。如果你想要 2014 年的事件,那么给它一个 2014 年的范围。
我认为发生的事情是我的设备上的设置在 iOS 升级过程中发生了变化(我不知道是如何发生的)。在 Settings/Calendar 中,您可以指定事件的同步方式。我发现它被设置为“1个月”。我现在能够检索超过 1 个月的事件。