This line:
[self performSelectorInBackground:@selector(mySel:) withObject:@YES];
is passing an NSNumber
instance. It is necessary to use an object rather than a scalar BOOL
value here because -performSelectorInBackground:withObject:
can only handle object-typed arguments (as "withObject:" suggests).
However, your method -mySel:
take a BOOL
parameter. Nothing in the framework will automatically unbox the NSNumber
to convert it to the BOOL
. Instead, your method is receiving an object pointer value but interpreting it as a BOOL
.
Now, BOOL
is an unsigned char
or, on some architectures, a bool
. So, only the low byte or low bit is examined by the "if" statement. A pointer to an NSNumber
is quite likely to have a zero low byte or bit. Note that it doesn't matter what the value of the NSNumber
object is. Only its address is being examined.
The debugger is probably confused. It may be doing the equivalent of "po <full 64-bit register value holding the object pointer>". So, even though your program is only examining the low byte or bit, the debugger is examining the full 64-bit value and treating it like an object pointer. Thus the debugger is misleading you as to what should happen.
As others have suggested, you can change your -mySel:
method to take an NSNumber*
rather than a BOOL
. You will have to use the -boolValue
method to examine that object's value.
However, you can also avoid this sort of issue by using Grand Central Dispatch (GCD) rather than -performSelectorInBackground:withObject:
. Your code could be:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self mySel:YES];
});
There's no need to box the YES
value into an object, so there's no problem with it being interpreted incorrectly in the method. And, if you did happen to have a type mismatch, the compiler can recognize it and warn you about it, which it can't do with -performSelector...
methods.
Or, if you don't really need the -mySel:
method except because you wanted to refer to it by a selector, you could directly inline its code into the block.