dugla
dugla

Reputation: 12954

How do I make a plain ole C-function an Objective-C selector?

I want to point to a plain ole C-function from Objective-C. Specifically, I am pointing to some OpenGL functions. I want to point to this function from an entry in an NSDictionary.

So is there a way to turn a C function pointer into a SEL?

Thanks,
Doug

Upvotes: 0

Views: 114

Answers (2)

Lily Ballard
Lily Ballard

Reputation: 185861

You could wrap it in an NSValue.

[dictionary setObject:[NSValue valueWithPointer:&myFunction] forKey:theKey];

Then when you extract it later, just call -pointerValue to get a void* pointer back (which you can cast to your function type).

Upvotes: 1

ericg
ericg

Reputation: 8742

There may be a better way, but why not have an Obj-C method that will call the plain ole C-Function?

You can then make the Obj-C method the SEL that you call.

Upvotes: 2

Related Questions