0

在 Xcode 中显示一个错误,显示“无法将 'NSCalendar.Unit' 类型的值转换为预期的参数类型 'Set'”。

出现此错误的代码行如下:

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

问题是我尝试过的“NSCalendar.Unit.year”不正确。如何在 Apple 的文档中搜索正确的解决方案?我尝试了 Google 搜索,但它会显示各种博客,但我正试图更倾向于从 Apple 的文档中寻找解决方案。

4

1 回答 1

1

首先,使用日历,而不是 NSCalendar。所以你想与之交谈Calendar.current

文档在这里:

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

您调用的方法在这里:

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

其次,只使用单位名称。Swift 已经知道这是一组 Calendar.Component。所以只需说[.year](一组文字)。

文档在这里:

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

于 2016-11-02T03:58:30.823 回答