2

I'm working on accessing healthkit's Heartrate data and through following the documentation here: https://developer.apple.com/reference/healthkit/hksamplequery. They call to use the following code snippet

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

However the debugger catches the NSCalendar.currentCalendar() as cannot call value of non-function type calendar. I changed it to NSCalendar.current as suggested by the IDE however then the third line has a problem stating that it cannot use instance member 'calendar' within property initializer.

So my question is what is the difference between NSCalendar.current and NSCalendar.currentCalendar() and what am I doing wrong?

Thanks so much for any help that you can provide.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • 1
    You need to convert to Swift 3. You are feeding Swift 2 code to the Swift 3 compiler. That's not going to work. They are different languages. Use Calendar, Date, and all their new properties and methods. – matt Oct 19 '16 at 00:56

1 Answers1

2

The difference is that you are using Swift 3 but looking at instructions for Swift 2.x. The APIs have changed. This is the Swift 3 equivalent:

let calendar = Calendar.current
let now = Date()
let components = calendar.dateComponents([.year, .month, .day], from: now)
Dima
  • 23,484
  • 6
  • 56
  • 83
  • 1
    Swift 3 you should use Calendar instead of NSCalendar – Leo Dabus Oct 19 '16 at 01:40
  • Thanks so much that helped! But it seems like the third line still has an issue: Cannot use instance member 'calendar' within property initializer; property initializers run before 'self' is available – Mark Gong-Guy Oct 20 '16 at 01:27
  • http://stackoverflow.com/questions/38248941/how-to-get-time-hour-minute-second-in-swift-3-using-nsdate – Mark Gong-Guy Oct 20 '16 at 01:38