我在 Swift 1.2 中有以下代码:
//Age
@IBOutlet weak var daysLabel: UILabel!
@IBOutlet weak var monthsLabel: UILabel!
@IBOutlet weak var yearsLabel: UILabel!
@IBAction func birthday(sender:AnyObject){
var dateComponents = NSDateComponents()
dateComponents.day = 19
dateComponents.month = 12
dateComponents.year = 1999
let birthDate = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.dateFromComponents(dateComponents)!
let durationDateComponents = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.components( [.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: birthDate, toDate: NSDate(), options: nil)
yearsLabel.text = "\(durationDateComponents.year)"
monthsLabel.text = "\(durationDateComponents.month)"
daysLabel.text = "\(durationDateComponents.day)"
}
这用于显示距我生日还有多少天,但这似乎在 Swift 2 中不起作用。我在
处收到错误 let durationDateComponents = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.components( [.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: birthDate, toDate: NSDate(), options: nil)
说“nil 与 NSCalenderOptions 的参数不兼容。
最佳答案
从 Swift 2 开始,NS_OPTIONS(例如 NSCalendarOptions)作为 OptionSetType 映射到 Swift,它提供了类似集合的接口(interface)。特别是,“无选项”现在可以指定为 [] 而不是 nil。 看这个answer了解更多详情。
关于swift - 倒计时代码在 Swift 2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32941275/