NSCalendar.current和NSCalendar.currentCalendar()

时间:2016-10-19 00:54:23

标签: swift3 nscalendar

我正在努力访问healthkit的Heartrate数据,并遵循以下文档:https://developer.apple.com/reference/healthkit/hksamplequery。他们打电话使用以下代码段

let calendar = NSCalendar.currentCalendar()
let now = NSDate()
let components = calendar.components([.Year, .Month, .Day], fromDate: now)

然而,调试器捕获NSCalendar.currentCalendar(),因为无法调用非函数类型日历的值。我按照IDE的建议将其更改为NSCalendar.current但是第三行有一个问题,即它不能使用实例成员' calendar'在属性初始化程序中。

所以我的问题是NSCalendar.current和NSCalendar.currentCalendar()之间的区别是什么?我做错了什么?

非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

不同之处在于您使用的是Swift 3,但查看了Swift 2.x的说明。 API已经改变了。这是Swift 3的等价物:

let calendar = Calendar.current
let now = Date()
let components = calendar.dateComponents([.year, .month, .day], from: now)