Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay:inModes:" rel="noreferrer">-[NSObject performSelector:withObject:afterDelay:inModes:] documentation</a>:</p> <p><strong>2013/11/01 Answer</strong></p> <p>The docs appear to be updated, as expected, now they don't say the target object is retained however they still mention an scheduled timer in the runloop.</p> <p>If an NSTimer is used then the object must be retained by somebody or there would be no guarantee the selector can be performed since no-one could assure the object would be still alive. If not using ARC's <code>weak</code> it will crash. In my opinion, Apple edited its documentation to not mention implementation details, although I think it is pretty obvious. </p> <p>Above document does NOT mention the word <em>retain</em> at all however, it does NOT mean the target is is not retained. </p> <p>This is what I tried in the debugger in the simulator iOS7.0:</p> <pre><code>(lldb) p (int)[self retainCount] (int) $1 = 8 (lldb) expr (void)[self performSelector:@selector(description) withObject:nil afterDelay:1.0] (lldb) p (int)[self retainCount] (int) $2 = 9 (lldb) expr (void)[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(description) object:nil] (lldb) p (int)[self retainCount] (int) $3 = 8 </code></pre> <p>Conclusion: the object is retained, but it is not something we should care about. Mister solved :p</p> <hr /> <p><strong>Previous Answer:</strong> (still valid)</p> <blockquote> <p>This method retains the receiver and the anArgument parameter until after the selector is performed.</p> </blockquote> <p>Because if the object is not retained, the object might be released before it gets executed and your app will crash. </p> <p>Is the same logic when you set and try to fire a method using NSTimers. When the timer is actually fired if the target object does not exist anymore (is released) your app will crash. So <code>performSelector:withObject:afterDelay:...</code> and its friends are here to make our life a little bit easier because it makes sure the app won't crash when the timer is fired ;)</p> <p>Hope it helps</p>
 

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