是一个简洁的 Swift 库,用于处理工作日。我们都讨厌处理工作日的情况,它们在代码中显得不太直观。这就是创建这个库的原因。
目前仍在开发中,但已在多个生产环境的应用中使用。遵循 SemVer 规范,不必担心破坏性更新。
.next
和 .previous
这样的方法,并支持一周的边界情况。import Weekday
let today = Weekday.current // it is monday for example
today.next == .tuesday
// when the week starts at sunday (USA, ...)
let saturday = Weekday.saturday
saturday.next == .sunday
// when the week starts at monday (Europe, ...)
let sunday = Weekday.sunday
sunday.next == .monday
// same for previous
let wednesday = Weekday.wednesday
wednesday.previous == .tuesday
// find first next working day (not today)
let friday = Weekday.friday
friday.firstNext(from: Weekday.workingDays) == .monday
Weekday.all.forEach {
let label = UILabel()
label.text = $0.localizedName
if $0.isToday {
label.textColor = .red
} else if Weekday.workingDays.contains($0) {
label.textColor = .white
} else {
label.textColor = .lightGray
}
// some other configuration
stackView.addArrangedSubview(label)
}
Weekday.swift 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到你的 Podfile 文件中
pod 'Weekday'
Weekday.swift 也可以通过 Swift Package Manager 获取。将以下行添加到你的 Package.swift
文件的依赖项中
.package(url: "https://github.com/samnung/Weekday.swift.git", .upToNextMajor(from: "0.2")),
MIT 许可。详情请参阅 LICENSE
文件。