基于SWOT单元的SWIFT地基测量框架 我如何使用基金会的度量()来显示日期迭代数年(或年+月)?我已经做了显示间隔,但我发现最大的单位是小时…它对我无效(显示10年间隔87600小时)
如何解决这个问题 我的代码:基于SWOT单元的SWIFT地基测量框架 我如何使用基金会的度量()来显示日期迭代数年(或年+月)?我已经做了显示间隔,但我发现最大的单位是小时…它对我无效(显示10年间隔87600小时),swift,xcode,foundation,units-of-measurement,measurement,Swift,Xcode,Foundation,Units Of Measurement,Measurement,如何解决这个问题 我的代码: // MODEL part var yearsLived: Measurement<UnitDuration>? { if let hours = birthDate?.getInterval(toDate: deathDate, component: .hour) { let hours = Measurement(value: Double(hours), unit: UnitDuration.hours) r
// MODEL part
var yearsLived: Measurement<UnitDuration>? {
if let hours = birthDate?.getInterval(toDate: deathDate, component: .hour) {
let hours = Measurement(value: Double(hours), unit: UnitDuration.hours)
return hours
}
return nil
}
// VC PART:
if let yearsLived = person?.yearsLived {
let formatter = MeasurementFormatter()
formatter.unitStyle = .long
formatter.unitOptions = [.naturalScale]
yearsLivedLabel.text = formatter.string(from: yearsLived)
}
//模型部件
风险值:测量?{
如果let hours=birthDate?.getInterval(toDate:deathDate,组件:.hour){
设小时=测量值(值:双倍(小时),单位:UnitDuration.hours)
返程时间
}
归零
}
//VC部分:
如果让yearsLived=人?yearsLived{
let formatter=MeasurementFormatter()
formatter.unitStyle=.long
formatter.unitOptions=[.naturalScale]
YearsLisvedLabel.text=格式化程序.string(发件人:YearsLisved)
}
我如何使用基金会的度量
在年(或年+月)中显示日期迭代?你没有。您可以使用。度量不是如何显示时间间隔。那么它是什么单位持续时间,或者像时间间隔?根据文档:持续时间是一个时间量。时间的国际单位制单位是秒,它是根据铯-133原子的放射性来定义的。持续时间通常以分钟(min)和小时(hr)表示。年/月/日只是另一个时间间隔表达式。文档中还说明了在年、月、周和日中使用DateComponents。@nysander您想争论还是想知道如何格式化时间间隔表达式?你的整个问题都被误导了。“如何使用基金会的度量值在年份(或年+月)中显示日期迭代?”你没有。我在寻找解决方案,而不是争论。谢谢你@matt,这很有效。对于是否使用度量值或DateComponents进行适当的本地化字符串,我有点误解,对此我深表歉意。现在,在你的帮助下,它可以完美地工作