I'm trying to get the current time, and I've done this so far:
let date = NSDate()
let calender = NSCalendar.currentCalendar()
let components = calender.component([.Hour, .Minute], fromDate: date)
And I then try to get the hour of the components
let hour = components.hour
This give me "Value of type "Int" has no member 'hour'". Any suggestions?
Change
let components = calender.component([.Hour, .Minute], fromDate: date)
to
let components = calender.components([.Hour, .Minute], fromDate: date)
NSCalendar.component(_:fromDate:)
returns a single component of a date, as an Int
. So your components
variable is actually of type Int
. Passing multiple units to component(_:fromDate:)
(as you are doing) is undefined.
Try this instead:
let components = calender.components([.Hour, .Minute], fromDate: date)
Note that the first part of the method name here is components
, not component
.
Also, you might want to change your calender
variable to calendar
, since that is the correct spelling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With