相关疑难解决方法(0)

dateByAddingComponents:toDate:options什么时候返回nil?

我一直在使用dateByAddingComponents:toDate:options:dateByAddingUnit:value:toDate:options:使用可选绑定来获取日期.像这样:

guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else {
    return nil
}
Run Code Online (Sandbox Code Playgroud)

其中oneMonthComponent只是一个月份值设置为1的NSDateComponent.

当我读到它时,在文档中他们都说了如下:

如果日期超出接收器的定义范围或者无法执行计算,则返回nil.

而且我一直在想,究竟是什么时候造成的.如果我只是添加一个月的日期,有没有办法可以是零?这是因为在其他日历系统中存在一些差异,其中添加一些单位没有意义吗?

我已经四处寻找并且无法找到一个返回nil的实例.

是否有任何可以使上面的代码为零?添加组件可能为零的示例是什么?

nsdate nscalendar nsdatecomponents ios swift

9
推荐指数
1
解决办法
496
查看次数

标签 统计

ios ×1

nscalendar ×1

nsdate ×1

nsdatecomponents ×1

swift ×1