Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>myObjCSelector:</code> and to get the unique selector for it you would type <code>@selector(myObjCSelector:)</code>. However this would be of no use to you because it doesnt represent a particular implementation of a function.</p> <p>What youre looking for is IMP. Refer to <a href="https://stackoverflow.com/questions/2650190/objective-c-and-use-of-sel-imp">this</a> SO question.</p> <p><strong>EDIT 2:</strong></p> <pre><code> IMP myObjCSelectorPointer = (void (*)(id,SEL,int*))[self methodForSelector:@selector(myObjCSelector:)]; </code></pre> <p>Then you can call the method using</p> <pre><code>myObjCSelectorPointer(self,@selector(myObjCSelector:),myIntArray); </code></pre> <p>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 </p> <pre><code>passObjCSelectorPointerToCContext(cContextReference, self, myObjCSelectorPointer); </code></pre> <p>when called from within the object that contains the method.</p> <p>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. </p> <p><strong>EDIT 1:</strong></p> <p>It's useful to understand why objc works in this way. The Apple documents <a href="http://developer.apple.com/library/iOS/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html" rel="nofollow noreferrer">explain</a> it in depth. However a short explanation is as follows:</p> <p>When you send a message to an object such as <code>[myobject somemethod]</code> 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. <code>[myobject somemethod]</code> gets converted by the compiler into a C function call:</p> <pre><code>objc_msgSend(myobject, @selector(somemethod)) </code></pre> <p>This is a special function that searches each <code>myobject</code> class layout to see whether that class knows how to respond to a <code>somemethod</code> message. If not it then searches that class's parent and so on until the root. If none of the classes can respond to <code>somemethod</code> then <code>NSObject</code> defines a private method called forward where all unknown messages are sent. </p> <p>Assuming that a class can respond to the <code>somemethod</code> 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.</p> <p>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.</p> <p>One final point is that the reason method names are mapped to unique integers via the <code>@selector</code> directive is so that the runtime doesn't have to waste time doing string comparisons.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload