Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All of these perform the same task, that is make the <code>doStuff</code> method on <code>anObject</code> execute synchronously on the <strong>current</strong> thread:</p> <pre><code>// 1 [anObject doStuff]; // 2 [anObject performSelector:@selector(doStuff)]; // 3 objc_msgSend(anObject, @selector(doStuff)); // 4 IMP imp = [anObject methodForSelector:@selector(doStuff)]; imp(anObject, @selector(doStuff)); </code></pre> <ol> <li>Is how you normally should go about to do stuff.</li> <li>Is for dynamically dispatching a message. Use if the selector is unknown, or provided by a client, for example if you implement an target-action pattern. or if the class of <code>anObject</code> is unknown, usually used by first asking if the object has the method with <code>-[NSObject respondsToSelector:]</code>.</li> <li>Is what no 1. is actually compiled down to. Usually never any real need to do this.</li> <li>Cached the actual <code>IMP</code> <em>(implementation)</em> for a method, and then call it directly. Can sometimes be faster than 1. if used in a tight loop. Just remember; <em>premature optimization is evil</em>.</li> </ol> <p>What you need to grasp is that in Objective-C methods are more important than classes/interfaces. Usually you do not query an object if it belongs to a particular class, or conforms to any protocol, that is for the compiler to complain about. At run-time you instead query for specific methods.</p> <p>In short: <strong>It does not matter what you are, just what you can do.</strong></p> <p>As a convenience <code>NSObject</code> also have several siblings to <code>performSelector</code> that are asynchronios. Most notably:</p> <ul> <li><code>performSelector:withObject:afterDelay:</code> - To execute the method on the current thread after a delay.</li> <li><code>performSelectorInBackground:withObject:</code> - To execute the method on a new background thread.</li> <li><code>performSelectorOnMainThread:withObject:waitUntilDone:</code> - To execute the method on the main thread.</li> <li><code>performSelector:onThread:withObject:waitUntilDone:</code> - To execute the method on any thread.</li> </ul> <p>The asynchronous performers all depend on a <code>NSRunLoop</code> to function. This is not something you need to worry about unless you spawn a thread yourself. If you do then you need to also run the new threads run loop. Just skip that for now.</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