Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Part II: Fundamental Patterns > Perform Selector and Delayed Perform

9. Perform Selector and Delayed Perform

Selectors identify the messages that are sent to Objective-C objects. They’re used by the receivers of messages to select which methods will be performed. Selectors provide much of the power, flexibility, and dynamism of the Objective-C language, and they’re used to implement other Cocoa design patterns. In particular, selectors are key to Cocoa’s implementation of the Notifications, Delegates, Targets and Actions, Invocations, and Forwarding patterns. Using selectors, Cocoa objects can be asked to perform methods immediately or after arbitrary time delays. Delaying the performance of methods can be very handy and is sometimes used to keep a user interface responsive while long-running tasks complete, implement animation, or provide other time-based application features.

Selectors are an object-oriented substitute for C function pointers. A function pointer is a variable that stores the address of a compiled function. In the C programming language and languages derived from it like Objective-C and C++, compilers and linkers convert explicit function calls in program source code into machine language jumps to code at predetermined fixed addresses in memory. The conversion of code to fixed addresses in sometimes called binding. Using a function pointer variable enables programmers to postpone binding of a function call until the program is running. For example, the value of a function pointer might be partly determined by user input at runtime. The technique is sometimes called late-binding. Just like using function pointers can postpone specification of precisely what function will be called, using selectors can postpone specification of precisely what message will be sent to an object.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial