As Andrey Chemukha already cited, you cannot cast an int *
to NSNumber *
.
Another viable option, if you really have a need to pass the pointer versus the contents of the pointer (which is what Andrey has provided code for) is use NSValue
. An example of why you may need this is if you have a deferred call but you want that call to have the most up to date value. Note I'm ignoring synchronization aspects here for simplicity.
And while you're at it you should clean up your usage of data types and use either a bool
or BOOL
to hold your flag.
- (void)start:(BOOL *)shouldPause {
NSValue *oShouldPause = [NSValue valueWithPointer:shouldPause];
[self performSelectorInBackground:@selector(runThread:) withObject:oShouldPause];
}
- (void)runThread:(NSValue *)value {
BOOL *shouldPause = (BOOL *) [value pointerValue];
if (shouldPause) {
// Pause-a-licous
NSLog(@"shouldPause is %d", *shouldPause);
}
}
NOTE THIS HAS CAVEATS!! Meaning, you need to guarantee the lifetime of the pointer else you will have a dangling pointer. So doing this is not for the meek.
To be clear, based on what you asked, I think Andrey's answer is the right thing to do. I'm just adding this as another option for your toolbox.
Also given how you are doing things, you don't even need to use performSelector:withObject:
.
- (void)startAlternate:(BOOL *)shouldPause {
[self runThreadAlternate:shouldPause ? *shouldPause : NO];
}
- (void)runThreadAlternate:(BOOL)shouldPause {
// Pause-o-matic
NSLog(@"shouldPause is %d", shouldPause);
}
I'd probably even then go as far as to change the argument to startAlternate:
be a BOOL
and not a pointer to BOOL
. More often than not, it's best to keep things simple.