Re: Class lookup at runtime
Re: Class lookup at runtime
- Subject: Re: Class lookup at runtime
- From: "John Engelhart" <email@hidden>
- Date: Fri, 7 Dec 2007 22:32:09 -0500
On Dec 7, 2007 4:41 AM, Wesley Smith <email@hidden> 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/Reference/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/Reference/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/index.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/index.html
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden