iosswiftnscalendarapple-documentation

Where can I find in Apple's Documentation the options for Set<Calendar.Component>?


In Xcode an error displays that reads "Cannot convert value of type 'NSCalendar.Unit' to expected argument type 'Set'".

The line of code that presents this error is the following:

let dateComponents = NSCalendar.current.dateComponents(NSCalendar.Unit.year, from: <Date>, to: <Date>)

The problem is "NSCalendar.Unit.year" which I tried that is incorrect. How can I search for the correct solution in Apple's documentation? I tried a Google search however it brings up various blogs however I'm trying to become more inclined in finding solutions from Apple's documentation.


Solution

  • First of all, use Calendar, not NSCalendar. So you want to talk to Calendar.current.

    Documentation is here:

    https://developer.apple.com/reference/foundation/calendar

    The method you are calling is here:

    https://developer.apple.com/reference/foundation/calendar/2292887-datecomponents

    Second, just use the name of the unit. Swift already knows this is a set of Calendar.Component. So just say [.year] (a set literal).

    Documentation is here:

    https://developer.apple.com/reference/foundation/calendar.component