0

Let's say I have the following code:

[somevalue_field 
  addTarget:self 
  action:@selector(somevalue_fieldDidChange:)
  forControlEvents:UIControlEventEditingDidEnd | UIControlEventTouchDragExit |
                   UIControlEVentTouchDragOutside ];

Will the bitwise ORing of these UIControlEvents actually work together in terms of combining their effects so that if either of these events happen, the method in the selector will fire?

Or would that result in too many bits being strung together to fit in one integer?

zeboidlund
  • 9,731
  • 31
  • 118
  • 180

1 Answers1

5

Yes you can OR the control events flags together.

I think you may be a little confused about how bitwise OR works. ORing does not increase the number of bits, it just increasse the number of set bits.

When you see flags defined like this:

UIControlEventTouchDragOutside    = 1 <<  3,
UIControlEventTouchDragExit       = 1 <<  5,
UIControlEventEditingDidEnd       = 1 << 18,

it is often a clue that it was intended they could be ORed together.

In binary these flags and the result of the OR would be:

UIControlEventTouchDragOutside 00000000000000000000000000001000
UIControlEventTouchDragExit    00000000000000000000000000100000
UIControlEventEditingDidEnd    00000000000001000000000000000000

Result of OR                   00000000000001000000000000101000

So as you can see, the number of bits in the result is still the same (32) it's just the number of set bits that changes.

Google "binary arithmetic" and "bitwise boolean operators" for more on this.

A final note, the Apple docs for addTarget:action:forControlEvents say:

controlEvents A bitmask specifying the control events for which the action message is sent. See “Control Events” for bitmask constants.

The term bitmask implies that you can OR together the Control Event constants for this call.

idz
  • 12,825
  • 1
  • 29
  • 40