Previous Section Next Section Table of Contents Glossary Index

Chapter 14. The Objective-C Bridge

14.2. Using Objective-C Classes

The class of most standard CLOS classes is named STANDARD-CLASS. In the Objective-C object model, each class is an instance of a (usually unique) metaclass, which is itself an instance of a "base" metaclass (often the metaclass of the class named "NSObject".) So, the Objective-C class named "NSWindow" and the Objective-C class "NSArray" are (sole) instances of their distinct metaclasses whose names are also "NSWindow" and "NSArray", respectively. (In the Objective-C world, it's much more common and useful to specialize class behavior such as instance allocation.)

When Clozure CL first loads foreign libraries containing Objective-C classes, it identifies the classes they contain. The foreign class name, such as "NSWindow", is mapped to an external symbol in the "NS" package via the bridge's translation rules, such as NS:NS-WINDOW. A similar transformation happens to the metaclass name, with a "+" prepended, yielding something like NS:+NS-WINDOW.

These classes are integrated into CLOS such that the metaclass is an instance of the class OBJC:OBJC-METACLASS and the class is an instance of the metaclass. SLOT-DESCRIPTION metaobjects are created for each instance variable, and the class and metaclass go through something very similar to the "standard" CLOS class initialization protocol (with a difference being that these classes have already been allocated.)

Performing all this initialization, which is done when you (require "COCOA"), currently takes several seconds; it could conceivably be sped up some, but it's never likely to be fast.

When the process is complete, CLOS is aware of several hundred new Objective-C classes and their metaclasses. Clozure CL's runtime system can reliably recognize MACPTRs to Objective-C classes as being CLASS objects, and can (fairly reliably but heuristically) recognize instances of those classes (though there are complicating factors here; see below.) SLOT-VALUE can be used to access (and, with care, set) instance variables in Objective-C instances. To see this, do:

      ? (require "COCOA")
    

and, after waiting a bit longer for a Cocoa listener window to appear, activate that Cocoa listener and do:

? (describe (ccl::send ccl::*NSApp* 'key-window))
    

This sends a message asking for the key window, which is the window that has the input focus (often the frontmost), and then describes it. As we can see, NS:NS-WINDOWs have lots of interesting slots.


Previous Section Next Section Table of Contents Glossary Index