CVCalendar DayView.date 错误

CVCalendar DayView.date Error

我正在使用来自 GitHub 的 Mozharovsky 的 CVCalendar。 我正在尝试使用此方法标记特定日期:

func supplementaryView(shouldDisplayOnDayView dayView: DayView) -> Bool
{
    if(dayView.date.day == day && dayView.date.month == month) {
        return true
    }

    return false
}

问题是 dayView.date 有时为 nil,这会导致错误:

fatal error: unexpectedly found nil while unwrapping an Optional value 

如何避免 nils 和错误?

只需使用基本的 Optional Unwrapping 来测试它是否已定义并执行适合您需要的操作:

func supplementaryView(shouldDisplayOnDayView dayView: DayView) -> Bool
{
    if let date = dayView.date {
        if(date.day == day && date.month == month) {
            return true
        }
    } else {
        // date is nil :(
    }

    return false
}