tags:

views:

535

answers:

1

I want to pass a point to a C++ object to the selector invoked using performSelectorOnMainThread. Simply casting the pointer to an 'id' (objc_object*) doesn't seem to work. I could always write my own objective C wrapper class, but there should be a fairly standard way of doing this. I didn't really find anything on apple's documentation for it though.

What's the best way to do this?

Here's what I want to do:

...
Foo *foo = new Foo(); // Foo is a C++ class
MyObject *myObj = [[MyObject alloc] init]; // Obj-C class
[myObj performSelectorOnMainThread:@selector(someMethod:) withObject:foo waitUntilDone:NO];
...

Thanks!

+5  A: 

No need to make your own wrapper class; [NSValue valueWithPointer:] is designed for boxing pointers. Just make someMethod take an NSValue rather than a raw pointer, and then extract the pointer with pointerValue.

(I believe that the performSelector* methods retain their arguments so that the object is guaranteed to last through the call, which is why you can't just cast a random value to an id in that argument.)

smorgan
This sounds correct - however for me it always worked to simply typecast non-object pointers to an id. Guess I am on thin ice ;)
Till