NSDateComponents语法有区别吗? [英] Differences in NSDateComponents syntax?

查看:194
本文介绍了NSDateComponents语法有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Swift开发时钟应用程序,并使用Xcode 6.3.2构建并运行以下代码.

// Get current time
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitNanosecond, fromDate: date)
let hour = components.hour % 12
let minute = components.minute
let second = components.second
let nanosecond = components.nanosecond

但是,当我在Xcode 7.0 beta中加载相同的项目并且不进行任何更改时,在calendar.components行上会出现错误.

Could not find member 'CalendarUnitHour'

我查看了文档,并且不推荐使用所有NSCalendarUnit常量(在iOS 8.0中它说),但是components方法的方法说明仍然说要使用它们.

我使用了其他NSCalendarUnit自动完成值,但是没有一个产生工作代码,并且我找不到在线最近的示例,也许是因为这是全新的.

有人知道这样做的新正确方法吗?

解决方案

Swift 2

NSCalendarUnit名称在Swift 2中已更改.

此外,现在我们必须在OptionSet中传递这些参数,如下所示:

let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date)

快捷键3

根据 Swift API设计指南. >

更新的语法:

let date = Date()
let calendar = Calendar.current()
let components = calendar.components([.hour, .minute, .second, .nanosecond], from: date)

快捷键4

Calendar.current现在是一个属性,并且.components已重命名为.dateComponents.否则,它与Swift 3中的相同.

let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .nanosecond], from: date)

I have been working on a clock app in Swift and with Xcode 6.3.2 the following code builds and runs just fine.

// Get current time
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitNanosecond, fromDate: date)
let hour = components.hour % 12
let minute = components.minute
let second = components.second
let nanosecond = components.nanosecond

However when I load the same project up in Xcode 7.0 beta and make no changes whatsoever, I get an error on the calendar.components line.

Could not find member 'CalendarUnitHour'

I've looked at the documentation and all of the NSCalendarUnit constants are deprecated (in iOS 8.0 it says) but the method description for the components method still says to use them.

I've played around with other NSCalendarUnit auto-complete values but none produce working code and I can't find any recent examples online, perhaps because this is brand new.

Anyone know the new correct way to do this?

解决方案

Swift 2

The NSCalendarUnit names have changed in Swift 2.

Also, now we have to pass these arguments in an OptionSet, like this:

let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date)

Swift 3

Many things have changed, according to the Swift API Design Guidelines.

Updated syntax:

let date = Date()
let calendar = Calendar.current()
let components = calendar.components([.hour, .minute, .second, .nanosecond], from: date)

Swift 4

Calendar.current is now a property, and .components has been renamed to .dateComponents. Otherwise it's the same as in Swift 3.

let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .nanosecond], from: date)

这篇关于NSDateComponents语法有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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