user6510422 user6510422 - 11 months ago 63
iOS Question

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.

Answer Source

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

Documentation is here:

The method you are calling is here:

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: