xcode - swift 2.0 : Value of type 'NSDateComponentsFormatter' has no member 'Day'

标签 xcode swift

我在日、小时和分钟收到错误。这是代码:

let dayCalendarUnit = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: date)


//here we change the seconds to hours,minutes and days
let CompetitionDayDifference = NSDateComponentsFormatter()
CompetitionDayDifference.unitsStyle = .Full
CompetitionDayDifference.allowedUnits = [.Day, .Hour, .Minute]
let string = CompetitionDayDifference.stringFromDate(currentDate!, toDate: competitionDay)

//finally, here we set the variable to our remaining time
var daysLeft = CompetitionDayDifference.Day
var hoursLeft = CompetitionDayDifference.Hour
var minutesLeft = CompetitionDayDifference.Minute

最佳答案

发生错误的原因是您尝试直接从日期格式化程序获取日期组件属性,这是不可能的。

实际上你不需要日期格式化程序。只需从差异中获取组件即可

let competitionDayDifference = calendar.components([.Day, .Hour, .Minute],
    fromDate: currentDate!, toDate: competitionDay, options: NSCalendarOptions())

//finally, here we set the variable to our remaining time
let daysLeft = competitionDayDifference.day
let hoursLeft = competitionDayDifference.hour
let minutesLeft = competitionDayDifference.minute

关于xcode - swift 2.0 : Value of type 'NSDateComponentsFormatter' has no member 'Day' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787433/

相关文章:

xcode - Swift 2.0 中如何获取另一个字符串中两个文本 block 之间的子字符串

ios - 锂聚合物 : input file must be a fat file

swift - 使用 Struct 传递变量 Swift

ios - 从 Firebase 打印数据

SwiftUI - 更改 TabBar 图标颜色

swift - 使用 Alamofire 上传文件

xcode - 如何将 UI 元素连接到 IBOutlets 和 IBActions?

XcodeServer一次只能使用一台模拟设备报错

ios - 添加到 viewDidLayoutSubviews 时按钮渐变不起作用

iphone - stretchableImageWithLeftCapWidth 导致角落模糊