Here’s the code:
for(int i = 0; i < personListViewController.peopleCount; i++) {
[NSThread detachNewThreadSelector:@selector(getPerson:) toTarget:self withObject:i];
}
getPerson looks like this:
- (void)getPerson:(int)whichPerson { }
When I build this, I get the following :warning: passing argument 3 of ‘detachNewThreadSelector:toTarget:withObject:’ makes pointer from integer without a cast
All I want to do is pass an int to getPerson via detachNewThreadSelector and I can’t figure out how the heck to get it to take anything but an object pointer. What am I missing here?
The selector you pass as the first argument to
-[NSThread detachNewThreadSelector:toTarget:withObject:]must take a single argument which is of typeid(a pointer to an object instance). Although you can play games with the fact that (on most platforms), a pointer is the same size as anintand type cast your value into anidand then back to anint, you should not rely on this behavior. Instead, write a wrapper method that takes anNSNumberargument and wrap yourintin anNSNumber. The wrapper method implementation could be:Your loop would then be
Note that I’ve converted from
inttoNSIntegerthroughout. Unless you are interfacing with legacy code that usesint, you should useNSIntegerwhich is a typedef for the appropriate size integer on each platform (32 or 64-bit) in new Cocoa code.