Class lookup at runtime

  • Hi,
    I'm trying to figure out how to dynamically get access to a class
    instance from a string.  Let's say I have a C string with
    "NSRectangle" in it.  Is there a way to find the NSRectangle class and
    then call alloc on it to generate an instance?  I see how to get a
    Selector from a string, but I haven't found anything about getting a
    class object.  Is there some part of the Obj-C runtime I can ask for
    this info?

    thanks,
    wes
  • On 07/12/2007, at 7:41 PM, Wesley Smith wrote:

    > Hi,
    > I'm trying to figure out how to dynamically get access to a class
    > instance from a string.  Let's say I have a C string with
    > "NSRectangle" in it.  Is there a way to find the NSRectangle class and
    > then call alloc on it to generate an instance?  I see how to get a
    > Selector from a string, but I haven't found anything about getting a
    > class object.  Is there some part of the Obj-C runtime I can ask for
    > this info?

    You're looking for the Foundation function NSClassFromString().

    --
    Rob Keniger
  • You can use this runtime functions to convert class names

    id    someObj = [[NSClassFromString(<some class name>) alloc] init]

    More here

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
    Miscellaneous/Foundation_Functions/Reference/reference.html#//
    apple_ref/doc/uid/20000055-BCIIIJCI

    On Dec 7, 2007, at 4:41 AM, Wesley Smith wrote:

    > Hi,
    > I'm trying to figure out how to dynamically get access to a class
    > instance from a string.  Let's say I have a C string with
    > "NSRectangle" in it.  Is there a way to find the NSRectangle class and
    > then call alloc on it to generate an instance?  I see how to get a
    > Selector from a string, but I haven't found anything about getting a
    > class object.  Is there some part of the Obj-C runtime I can ask for
    > this info?
    >
    > thanks,
    > wes
  • On Dec 7, 2007 4:41 AM, Wesley Smith <wesley.hoke...> wrote:
    > Hi,
    > I'm trying to figure out how to dynamically get access to a class
    > instance from a string.  Let's say I have a C string with
    > "NSRectangle" in it.  Is there a way to find the NSRectangle class and
    > then call alloc on it to generate an instance?  I see how to get a
    > Selector from a string, but I haven't found anything about getting a
    > class object.  Is there some part of the Obj-C runtime I can ask for
    > this info?

    Others have posted the Foundation wrapper / equivalents, but if you
    want the low-level functions that are ultimately called, you're
    probably looking for something like:

    objc_getClass("NSRectangle")

    which you could use as such:

    id rectangle = [[objc_getClass("NSRectangle") alloc] init];

    if(rectangle != nil) { NSLog(@"The NSRectangle class was found and an
    object of the class was successfully instantiated."); }
    else {
      if(objc_getClass("NSRectangle") == nil) { NSLog(@"Unable to locate
    the NSRectangle class."); }
      else { NSLog(@"The NSRectangle class was found, but could not
    instantiate an object from the class."); }
    }

    An example using NSGarbageCollector, which is present in 10.5, but not 10.4:

    objc_getClass("NSGarbageCollector")

    You can use the result from the function as if you had just the
    'NSGarbageCollector' class symbol, for example:

    // Call the defaultCollector class method for the NSGarbageCollector class

    if([objc_getClass("NSGarbageCollector") defaultCollector] != NULL) {
    /* The NSGarbageCollector class was found, and there is a
    defaultCollector object which in this case means that garbage
    collection is enabled and active. */ }

    This comes from working code that needs to run on 10.4, 10.5, and the
    GNUstep ObjC runtime and support both GC and non-GC.  One can almost
    think of this as the equivalent to the dlsym() function, which is used
    for getting the address of library and executable symbols dynamically
    at run time.  Since it is a function call to look up the class, it's
    fine for one shot uses, but if you're going to be repeatedly using the
    result you should think about caching it to avoid the overhead.  In a
    pure mac environment, you can use some weak linking tricks to get the
    same effect, but the above is more portable to GNUstep and the many
    platforms it runs on without requiring any assistance from the linker.

    You could cache the result with something like:

    id myNSGarbageCollector = nil; // Global variable

      ...
      myNSGarbageCollector = objc_getClass("NSGarbageCollector"); // At
    class initialization
      ...

    if([myNSGarbageCollector  defaultCollector] != NULL) { /* Example usage */ }

    Refer to http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Ref
    erence/reference.html#//apple_ref/doc/uid/TP40001418-CH1g-objc_getClass

    for information about the function in the ObjC 2.0 run time.

    You should also review objc_lookUpClass() as it has slightly different
    semantics than objc_getClass().  I'd recommend objc_getClass().
    objc_lookUpClass():
    http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Ref
    erence/reference.html#//apple_ref/doc/uid/TP40001418-CH1g-objc_lookUpClass


    The primary reference material:

    ObjC 2.0 Runtime Reference:
    http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/ind
    ex.html

    ObjC 2.0 Release Notes:
    http://developer.apple.com/releasenotes/Cocoa/RN-ObjectiveC/index.html
    ObjC 1.0 Runtime Reference:
    http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef1/in
    dex.html
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today