【问题标题】:CVCalendar DayView.date ErrorCVCalendar DayView.date 错误
【发布时间】:2015-07-28 13:11:18
【问题描述】:

我正在使用来自 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 和错误?

【问题讨论】:

    标签: ios xcode swift github calendar


    【解决方案1】:

    只需使用基本的 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多