Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Selector pointer to be passed to a C function

I have a C struct that contains a function pointer. Now, I have used this setup within C with no problems, but now I'm using this C struct in Objective-C and I need to pass a function (or selector) pointer that is defined in the Objective-C class.

1. Here is what I have for the Objective-C selector that needs to be passed as a pointer to the C function:

- (void)myObjCSelector:(int*)myIntArray
{
    // Do whatever I need with myIntArray
}

2. And here is where I run into a wall, Within Objective-C I'm trying to pass the selector as a pointer to the C function call: In place of "myObjCSelectorPointer" I need the proper syntax to pass the selector as a function pointer in this C function call:

passObjCSelectorPointerToCContext(cContextReference, myObjCSelectorPointer);

I did investigate this issue, but could mainly find several different ways of doing similar things, but I couldn't find anything specific for calling C functions and passing an Objective-C selector pointer.

like image 821
Claudia Avatar asked Aug 13 '11 15:08

Claudia


People also ask

What are selectors Objective-C?

In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it's used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled.

Can I use C in Objective-C?

You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.

Does Objective-C use pointers?

Objective-C allows you to have pointer on a pointer and so on. Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function.


3 Answers

In objc a selector is not a function pointer. A selector is a unique integer that is mapped to a string in a method lookup table stored by the objc runtime. In the above case your method name would be myObjCSelector: and to get the unique selector for it you would type @selector(myObjCSelector:). However this would be of no use to you because it doesnt represent a particular implementation of a function.

What youre looking for is IMP. Refer to this SO question.

EDIT 2:

 IMP myObjCSelectorPointer = (void (*)(id,SEL,int*))[self methodForSelector:@selector(myObjCSelector:)];

Then you can call the method using

myObjCSelectorPointer(self,@selector(myObjCSelector:),myIntArray);

However, what this means you will need to make sure that you add the pointer to self in the c function call passObjCSelectorPointerToCContext. So it should look like this

passObjCSelectorPointerToCContext(cContextReference, self, myObjCSelectorPointer); 

when called from within the object that contains the method.

It is important to note though that using IMP is almost never the right technique. You should try to stick with pure Obj-C. Obj-C is quite efficient after the first call to a message because it uses temporal caching.

EDIT 1:

It's useful to understand why objc works in this way. The Apple documents explain it in depth. However a short explanation is as follows:

When you send a message to an object such as [myobject somemethod] the compiler won't immediately know which particular implementation of somemethod to call because there might be multiple classes with multiple overriden versions of somemethod. All of those methods have the same selector, irrespective of its arguments and return values and hence the decision about which implementation of somemethod is deffered to when the program is running. [myobject somemethod] gets converted by the compiler into a C function call:

objc_msgSend(myobject, @selector(somemethod))

This is a special function that searches each myobject class layout to see whether that class knows how to respond to a somemethod message. If not it then searches that class's parent and so on until the root. If none of the classes can respond to somemethod then NSObject defines a private method called forward where all unknown messages are sent.

Assuming that a class can respond to the somemethod message then it will also have a particular pointer of type IMP that points to the actual implementation of the method. At that point the method will be called.

There is considerably more to this procedure than I have described but the outline should be enough to help you understand what the goal of a selector is.

One final point is that the reason method names are mapped to unique integers via the @selector directive is so that the runtime doesn't have to waste time doing string comparisons.

like image 156
twerdster Avatar answered Sep 20 '22 02:09

twerdster


Basically, the answer is: Objective-C selectors are different from function pointers. You need two pieces of data to perform a selector. That is an object and the selector itself. You will need some glue to accomplish your task.

Check this question.

like image 24
Anton Avatar answered Sep 20 '22 02:09

Anton


Do you have to use a function pointer? In Objective-C, you can get the function pointer to an arbitrary method implementation (known as an IMP), but this is extremely uncommon, and usually not a good idea. Calling objc_msgSend() directly is also not the greatest idea, because there are several different variants of objc_msgSend(), and the compiler automatically chooses different ones to use based on the return type of the method. Methods that return an object go through objc_msgSend(), but objects that return structs might go through objc_msgSend() or they might go through objc_msgSend_stret(). And if the method returns a double, then it goes through objc_msgSend_fpret()...

Documentation: Objective-C Runtime Reference: Sending Messages

Instead, I might recommend using a target-action pair, or using a block. Then you might do something like:

myContextRef->target = anObjcObject;
myContextRef->action = @selector(invokeMe:);

And when you're done, do:

[myContextRef->target performSelector:myContextRef->action withObject:someReturnInformation];

Or maybe use a block:

myContextRef->completionHandler = [^(id returnInformation) {
  [anObjcObject invokeMe:returnInformation];
} copy];

And then when you're done, do:

myContextRef->completionHandler(someReturnInformation);

(and don't forget to -release the block when you free the context)

like image 29
Dave DeLong Avatar answered Sep 20 '22 02:09

Dave DeLong