1

I have a table view with days from Monday to Sunday. I can use a bitmask to represent select/unselect all of them.

For example, if I select all of them, I will have 1111111 in binary or 127 in decimal. I found an enums example but can't recognize how to get an Integer from enums and then convert that number into selected days again if needed.

Let's say I selected all days in table, then I leave that table and reopen it one more time. So in my data I have 127 which represent all days that I selected (it can be 2 or 3 days selected for example). So I want to check mark those days as selected. But I have just a decimal.

What is the better way to do it?

And how to write switch

That should be kind like this I guess:

func markDaysAsSelctedWith(number: Int)
{
    switch (number) {
    case Days.Monday
    case Days.Sunday
}

Here is my problem: if I use the number 12 to represent the selected days Wednesday and Thursday:

M = 1,
Tu = 2,
W = 4,
Th = 8,
F = 16,
Sa = 32,
Su = 64

then switch will not work...

Jerry
  • 966
  • 2
  • 13
  • 28
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • 1
    Take a look at this example using `OptionSet` http://stackoverflow.com/a/24066171/2099148 – M_G Sep 14 '16 at 12:30

2 Answers2

3

In your case, consider adopting OptionSetType.

With declaring a struct conforming to OptionSetType:

(Assuming you are still using Swift 2.)

struct Days: OptionSetType {
    var rawValue: Int
    init(rawValue: Int) {self.rawValue = rawValue}

    static let Monday = Days(rawValue: 1<<0)
    static let Tuesday = Days(rawValue: 1<<1)
    static let Wednesday = Days(rawValue: 1<<2)
    static let Thursday = Days(rawValue: 1<<3)
    static let Friday = Days(rawValue: 1<<4)
    static let Saturday = Days(rawValue: 1<<5)
    static let Sunday = Days(rawValue: 1<<6)
}

You can write something like this:

func markDaysAsSelectedWith(days: Days)
{
    if days.contains(.Monday) {
        print("Monday marked")
    }
    if days.contains(.Tuesday) {
        print("Tuesday marked")
    }
    if days.contains(.Wednesday) {
        print("Wednesday marked")
    }
    if days.contains(.Thursday) {
        print("Thursday marked")
    }
    if days.contains(.Friday) {
        print("Friday marked")
    }
    if days.contains(.Saturday) {
        print("Saturday marked")
    }
    if days.contains(.Sunday) {
        print("Sunday marked")
    }
}

And use it as:

markDaysAsSelectedWith([.Monday,.Tuesday])
//Output:
//Monday marked
//Tuesday marked
let number = 12
let days = Days(rawValue: number)
markDaysAsSelectedWith(days)
//Output:
//Wednesday marked
//Thursday marked

(ADDITION)how to get number from selected days

As already noted in Martin R's comment, you can retrieve underlying rawValue with rawValue property:

let otherDays: Days = [.Wednesday,.Thursday]
print(otherDays.rawValue) //->12
OOPer
  • 47,149
  • 6
  • 107
  • 142
2

For each table view row (0...6) check if the bit at the corresponding position in the bit mask it set or not, and select or deselect the row accordingly. Example:

let selectedWeekdays = 69 // == 1 + 4 + 64

for i in 0..<7 {
    if selectedWeekdays & (1 << i) != 0 {
        print("select row #", i)
    } else {
        print("deselect row #", i)
    }
}

Output:

select row # 0
deselect row # 1
select row # 2
deselect row # 3
deselect row # 4
deselect row # 5
select row # 6
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382