Xcodeの8•スウィフト3
extension Calendar {
static let gregorian = Calendar(identifier: .gregorian)
}
extension Date {
var startOfWeek: Date {
return Calendar.gregorian.date(from: Calendar.gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))!
}
var nextSunday: Date {
return Calendar.gregorian.date(byAdding: DateComponents(weekOfYear: 1), to: startOfWeek)!
}
}
let firstSunday = Date().nextSunday
var result = [firstSunday]
(1...52).forEach { _ in
guard let nextSunday = result.last?.nextSunday else { return }
result.append(nextSunday)
}
print(result) // "[2017-06-11 03:00:00 +0000, 2017-06-18 03:00:00 +0000, 2017-06-25 03:00:00 +0000, 2017-07-02 03:00:00 +0000, 2017-07-09 03:00:00 +0000, 2017-07-16 03:00:00 +0000, 2017-07-23 03:00:00 +0000, 2017-07-30 03:00:00 +0000, 2017-08-06 03:00:00 +0000, 2017-08-13 03:00:00 +0000, 2017-08-20 03:00:00 +0000, 2017-08-27 03:00:00 +0000, 2017-09-03 03:00:00 +0000, 2017-09-10 03:00:00 +0000, 2017-09-17 03:00:00 +0000, 2017-09-24 03:00:00 +0000, 2017-10-01 03:00:00 +0000, 2017-10-08 03:00:00 +0000, 2017-10-15 03:00:00 +0000, 2017-10-22 03:00:00 +0000, 2017-10-29 02:00:00 +0000, 2017-11-05 02:00:00 +0000, 2017-11-12 02:00:00 +0000, 2017-11-19 02:00:00 +0000, 2017-11-26 02:00:00 +0000, 2017-12-03 02:00:00 +0000, 2017-12-10 02:00:00 +0000, 2017-12-17 02:00:00 +0000, 2017-12-24 02:00:00 +0000, 2017-12-31 02:00:00 +0000, 2018-01-07 02:00:00 +0000, 2018-01-14 02:00:00 +0000, 2018-01-21 02:00:00 +0000, 2018-01-28 02:00:00 +0000, 2018-02-04 02:00:00 +0000, 2018-02-11 02:00:00 +0000, 2018-02-18 03:00:00 +0000, 2018-02-25 03:00:00 +0000, 2018-03-04 03:00:00 +0000, 2018-03-11 03:00:00 +0000, 2018-03-18 03:00:00 +0000, 2018-03-25 03:00:00 +0000, 2018-04-01 03:00:00 +0000, 2018-04-08 03:00:00 +0000, 2018-04-15 03:00:00 +0000, 2018-04-22 03:00:00 +0000, 2018-04-29 03:00:00 +0000, 2018-05-06 03:00:00 +0000, 2018-05-13 03:00:00 +0000, 2018-05-20 03:00:00 +0000, 2018-05-27 03:00:00 +0000, 2018-06-03 03:00:00 +0000, 2018-06-10 03:00:00 +0000]\n"
翻訳に問題があるこのコードの特定の部分はありますか? –
はい、私はこのコードを変換したいので、すべての日曜日を1つの配列に入れることができます –