Xcode 8 beta 4:Calendar.Unit与Calendar.Component [英] Xcode 8 beta 4: Calendar.Unit vs Calendar.Component

查看:89
本文介绍了Xcode 8 beta 4:Calendar.Unit与Calendar.Component的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此Swift 3代码在XCode 8 beta 3之前一直有效:

This Swift 3 code worked until XCode 8 beta 3:

let calendar = Calendar.current
let anchorComponents = calendar.components([Calendar.Unit.day, Calendar.Unit.month, Calendar.Unit.year, Calendar.Unit.hour], from: self)

在Xcode 8 beta 4中,Calendar.Unit似乎已重命名为Calendar.Component.

In Xcode 8 beta 4 Calendar.Unit appears to be renamed to Calendar.Component.

现在输入此代码

let calendar = Calendar.current
let anchorComponents = calendar.components([Calendar.Component.day, Calendar.Component.month, Calendar.Component.year, Calendar.Component.hour], from: self)

产生编译器错误cannot convert value of type Calendar.Component to NSCalendar.Unit

我做错什么了吗,或者这是一个错误?

Am I doing anything wrong or is this a bug?

推荐答案

在Xcode 8 beta 4附带的Swift版本中,components已重命名为dateComponents.

In the Swift version shipped with Xcode 8 beta 4, components has been renamed to dateComponents.

注意:为简化通话,您可以省略Calendar.Component前缀.

Note: to make the call simpler, you can omit the Calendar.Component prefix.

let anchorComponents = calendar.dateComponents([.day, .month, .year, .hour], from: self)

您收到的错误消息有点令人误解,我想编译器正在努力进行类型推断.

The error message you got is a bit misleading, I guess the compiler was struggling with type inference.

这篇关于Xcode 8 beta 4:Calendar.Unit与Calendar.Component的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆