EKEvent endDate为零;无法在不崩溃应用程序的情况下检查隐式展开的可选项

时间:2017-10-23 14:30:10

标签: ios swift swift3 eventkit

在我的代码中,我从EKEventstore检索一个事件,然后将值映射到一个自定义的Swift对象

public class WPEvent: NSObject, NSCopying, Comparable {

let ddLogLevel = DDLogLevel.verbose

static let vacationPrefix = "Vacation"
static let visitReportPrefix = "* "

public var eventIdentifier: String
public var calendar: EKCalendar
public var title: String
public var allDay: Bool
public var detached: Bool
public var unsafed: Bool
public var creationDate: Date?
public var startDate: Date
public var endDate: Date
public var timeZone: TimeZone?
public var location: String?

public init(event: EKEvent) {
    eventIdentifier = event.eventIdentifier
    calendar = event.calendar
    title = event.title
    allDay = event.isAllDay
    detached = event.isDetached
    unsafed = event.isNew
    creationDate = event.creationDate
    startDate = event.startDate
    endDate = event.endDate
    timeZone = event.timeZone
    location = event.location

    super.init()
}

在我的所有测试中,这都过去了,很好。然而,最近我开始收到一些像这样的奇怪的崩溃报告

Hardware Model:      iPad5,4
OS Version:          iOS 10.2.1 (14D27)
Report Version:      105

Exception Type:  SIGTRAP
Exception Subtype: undefined

Thread 12 name:
Thread 12 Crashed:
0   libswiftFoundation.dylib         0x000000010295179c 0x102948000 + 38812
1   CalendarSyncEngine               0x0000000101399520 specialized WPEvent.init(event:) (WPEvent.swift:49)
2   CalendarSyncEngine               0x00000001013844b8 specialized EKEventStoreWrapper.events(withStart:end:salesRepId:) (EventStore.swift:0)
3   CalendarSyncEngine               0x00000001013846fc specialized EKEventStoreWrapper.events(forDate:salesRepId:) (EventStore.swift:0)

第49行是分配endDate的地方,如果它崩溃,那么总是在那里。

即使EventKit API将其列为隐式展开的可选项,是否有可能获得nil-Enddate?

显然是的(感谢评论)

由于此值是从Apple框架中提供的,如何在没有崩溃的情况下检查这是否为零?

更新 复制很简单,如AppleDoc中所述“@discussion在你设置它之前,对于新事件,这将是零。” 所以只是不设置它,但我仍然无法弄清楚如何检查此事件是否没有结束而不会崩溃。

1 个答案:

答案 0 :(得分:1)

我最后在我的Swift框架中添加了一小段Objective-C代码,用于检查nil并使用一些值填充日期以避免崩溃