Passing pointers or integral types through performSelector

I am mixing Objective-C parts in a C ++ project (please do not argue about this, its cross-platform).
Now I want to call some C ++ functions or methods in the correct thread (i.e., Main thread) in cocoa environment.

My current approach transmits function pointers to instance Objective-C (obtained from NSObject) and on it performSelector/ performSelectorOnMainThread.
But performSelectorexpect objects as their arguments, since this can usually be wrapped?

Example:

 typedef void(*FnPtr)(void*);
 FnPtr fn;
 [someInstance performSelector:@selector(test:) withObject:fn];

... where test is declared as:

- (void)test:(FnPtr)fn;

I must add that I just started with Objective-C this week, so if there is a better way, I would also be happy to hear about it.
Also note that I don’t have access to the main loop or to any objects of the application, because the project is a browser plug-in (currently it is only for Safari on Mac).

+3
source share
2 answers

As smorgan answered here , it is NSValueintended as a container for scalar types C and Objective-C:

- (void)test:(NSValue*)nv
{
    FnPtr fn = [nv pointerValue];
    // ...
}

// usage:
NSValue* nv = [NSValue valueWithPointer:fn];
[someInstance performSelector:@selector(test:) withObject:nv];
+5
source

, /. , , (id) s. , . , NSValue.

0

Source: https://habr.com/ru/post/1722432/


All Articles