This is a simple OptionSet Demo written in swift.
import UIKit
struct Weekday: OptionSet, CustomStringConvertible {
let rawValue: Int
/// Monday
static let Monday = Weekday(rawValue: 1 << 2)
/// Tuesday
static let Tuesday = Weekday(rawValue: 1 << 3)
/// Wednesday
static let Wednesday = Weekday(rawValue: 1 << 4)
/// Thursday
static let Thursday = Weekday(rawValue: 1 << 5)
/// Friday
static let Friday = Weekday(rawValue: 1 << 6)
/// Saturday
static let Saturday = Weekday(rawValue: 1 << 7)
/// Sunday
static let Sunday = Weekday(rawValue: 1 << 1)
/// Weekday from Sunday to Saturday in an Array.
static let WeekInArray: [Weekday] = [.Sunday, .Monday, .Tuesday, .Wednesday, .Thursday, .Friday, .Saturday]
/// Weekday constant contains Sunday through Saturday.
static let Week: Weekday = [.Sunday, .Monday, .Tuesday, .Wednesday, .Thursday, .Friday, .Saturday]
var weekdayInCalendar: Int {
return Int(log2(Double(self.rawValue)))
}
/// A textual representation of this instance.
/// Using the first 3 characters of each enum case to present its textual meaning.
var description: String {
get {
switch self {
case Weekday.Monday:
return "Mon"
case Weekday.Tuesday:
return "Tue"
case Weekday.Wednesday:
return "Wed"
case Weekday.Thursday:
return "Thu"
case Weekday.Friday:
return "Fri"
case Weekday.Saturday:
return "Sat"
case Weekday.Sunday:
return "Sun"
default:
return "Unknown"
}
}
}
}
print("\(Weekday.Monday)")
print("\(Weekday.Week)")
print("\(Weekday.WeekInArray)")