【问题标题】:Where can I find in Apple's Documentation the options for Set<Calendar.Component>?在 Apple 的文档中哪里可以找到 Set<Calendar.Component> 的选项?
【发布时间】:2016-11-02 03:50:33
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: ios swift nscalendar apple-documentation


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多