5. Dynamic Creation
Cocoa utilizes many features of the underlying Objective-C runtime. One powerful feature is the ability to create instances of classes that did not exist at the time an application was compiled and dynamically load and link new classes at runtime. Scripting languages such as AppleScript, Tcl, Ruby, Perl, and Python use this technique to provide interfaces to Cocoa. The core of this pattern is the NSClassFromString()
function that returns the class object with a specified name. NSBundle
augments this by implementing the method –classNamed:
, which can dynamically load code for the class if necessary. Once the named class object is obtained, instances can be created.
Dynamic creation enables scripting language bridges, plug-ins, ...
Get Cocoa Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.