<< operator in objective c enum?

后端 未结 6 1190
别跟我提以往
别跟我提以往 2021-02-04 05:46

I was looking for something and got in to this enum is apple UITableViewCell.h.

I am sorry if this is trivial but I wonder/curious what is the point of this.

I

6条回答
  •  无人及你
    2021-02-04 06:16

    It's a common trick in C to use the bitwise shift operator in enum values to allow you to combine enumeration values with the bitwise or operator.

    That piece of code is equivalent to

    enum {
        UITableViewCellStateDefaultMask                     = 0,
        UITableViewCellStateShowingEditControlMask          = 1, // 01 in binary
        UITableViewCellStateShowingDeleteConfirmationMask   = 2  // 10 in binary
    };
    

    This allows you to bitwise or two or more enumeration constants together

     (UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask) // == 3 (or 11 in binary)
    

    to give a new constant that means both of those things at once. In this case, the cell is showing both an editing control and a delete confirmation control, or something like that.

提交回复
热议问题