Women in Technology

Hear us Roar



Article:
  Inside the Objective-C Runtime
Subject:   Adding categories at runtime
Date:   2003-02-26 14:48:41
From:   anonymous2
Is it possible to add categories at runtime and even override existing methods?
(and, if it is, could you please give a short example or point me / us to some information?)
Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Adding categories at runtime
    2003-03-20 17:02:19  eepstein [View]

    Take a look the the - (void)openAction:(id)sender
    method in the AppController.m file in the source for the RuntimeBrowser. (See http://source.prajnait.com/src_RuntimeBrowser.html for the RB home page.)

    The gist of dynamically loading a bundle into the rutime is a call to NSBundle's -load method.

    There are ways to directly install a single method into the runtime. One way to find out more is to study the Objective-C runtime implementation source code which is released as part of Apple's Darwin project.
  • Adding categories at runtime
    2003-02-26 15:15:46  eepstein [View]

    Yes, this can be done. You make calls directly into the runtime itself. I'll take a look to see if I've got an example hanging about and will post it here. The "trick" is telling the runtime to re-cache the methods for the class in question. This is done when a bundle is loaded, so the esaiest way to get this effect is to load the bundle with the methods you need... after all, unless you have the code pre-compiled into some other form (other than a bundle) or wrote in-line binary instructions (not recommended and very unportable), you had to have the code compiled at some point and may as well have put it into a bundle/framework. Then you just tell the Brundle/framework loader to load the bundle in question and the methods will appear. You can also, of course, implement a category method in any file that is linked into your executable and it will be available when the program is running.

    Am curious about the application of this technique, if you'd care to share.