Search code examples
objective-ccocoa-touchprimitive

Passing an int as the object for performSelectorOnMainThread:


I need to invoke:

[self performSelectorOnMainThread:@selector(chooseURL:) withObject:myIndex waitUntilDone:YES];

But my problem is that myIndex is an int, and NOT an object. Is there a workaround for that?


Solution

  • You have to wrap the int in an object since int is a primitive type and not an object. For example:

    [self performSelectorOnMainThread:@selector(chooseURL:) 
                           withObject:[NSNumber numberWithInt:myIndex] 
                        waitUntilDone:YES];
    

    Of course you will have to edit your chooseURL: method to accept a NSNumber instead of an int. To unwrap:

    int myInt = [myIndex intValue];