我有这个内部日期扩展似乎不再适用于Swift 3:
internal extension DateComponents {
func to12pm() {
self.hour = 12
self.minute = 0
self.second = 0
}
}
错误消息是:
无法分配属性:'self'是不可变的
如何在Swift 3中实现这一目标?
其他信息(如果需要):此日期扩展程序调用了它:
func endOfWeek(_ weekday: Int) -> Date? {
guard
let cal = Calendar.current,
var comp: DateComponents = (cal as Calendar).components([.weekOfYear], from: self)
else {
return nil
}
comp.weekOfYear = 1
comp.day -= 1
comp.to12pm()
return (cal as NSCalendar).date(byAdding: comp, to: self.startOfWeek(weekday)!, options: [])!
}
现在在Swift 3中看起来像这样:
func endOfWeek(_ weekday: Int) -> Date? {
let cal = Calendar.current
var comp = cal.dateComponents([.weekOfYear], from: self)
comp.weekOfYear = 1
comp.day? -= 1
//This does not have the "comp.to12pm()" that the Swift 2 version did
return cal.date(byAdding: comp, to: self.startOfWeek(weekday)!)!
}
答案 0 :(得分:1)
DateComponents现在是一个结构,它解释了这个错误的原因:
无法分配属性:' self'是不可改变的
您需要从self创建一个新的var,更改它并按如下方式返回:
-app
-templates
-admin
change_list.html