gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:09:27 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com