Previous Section Next Chapter Table of Contents Glossary Index

Chapter 14. The Objective-C Bridge

14.8. How Objective-C Names are Mapped to Lisp Symbols

There is a standard set of naming conventions for Cocoa classes, messages, etc. As long as they are followed, the bridge is fairly good at automatically translating between Objective-C and Lisp names.

For example, "NSOpenGLView" becomes ns:ns-opengl-view; "NSURLHandleClient" becomes ns:ns-url-handle-client; and "nextEventMatchingMask:untilDate:inMode:dequeue:" becomes (:next-event-matching-mask :until-date :in-mode :dequeue). What a mouthful.

To see how a given Objective-C or Lisp name will be translated by the bridge, you can use the following functions:

(ccl::objc-to-lisp-classname string)
(ccl::lisp-to-objc-classname symbol)
(ccl::objc-to-lisp-message string)
(ccl::lisp-to-objc-message string)
(ccl::objc-to-lisp-init string)
(ccl::lisp-to-objc-init keyword-list)

Of course, there will always be exceptions to any naming convention. Please tell us on the mailing lists if you come across any name translation problems that seem to be bugs. Otherwise, the bridge provides two ways of dealing with exceptions:

First, you can pass a string as the class name of MAKE-OBJC-INSTANCE and as the message to SEND. These strings will be directly interpreted as Objective-C names, with no translation. This is useful for a one-time exception. For example:

(ccl::make-objc-instance "WiErDclass")
(ccl::send o "WiErDmEsSaGe:WithARG:" x y)

Alternatively, you can define a special translation rule for your exception. This is useful for an exceptional name that you need to use throughout your code. Some examples:

(ccl::define-classname-translation "WiErDclass" wierd-class)
(ccl::define-message-translation "WiErDmEsSaGe:WithARG:" (:weird-message :with-arg))
(ccl::define-init-translation "WiErDiNiT:WITHOPTION:" (:weird-init :option))

The normal rule in Objective-C names is that each word begins with a capital letter (except possibly the first). Using this rule literally, "NSWindow" would be translated as N-S-WINDOW, which seems wrong. "NS" is a special word in Objective-C that should not be broken at each capital letter. Likewise "URL", "PDF", "OpenGL", etc. Most common special words used in Cocoa are already defined in the bridge, but you can define new ones as follows:

(ccl::define-special-objc-word "QuickDraw")

Note that message keywords in a SEND such as (SEND V :MOUSE P :IN-RECT R) may look like the keyword arguments in a Lisp function call, but they really aren't. All keywords must be present and the order is significant. Neither (:IN-RECT :MOUSE) nor (:MOUSE) translate to "mouse:inRect:"

Also, as a special exception, an "init" prefix is optional in the initializer keywords, so (MAKE-OBJC-INSTANCE 'NS-NUMBER :INIT-WITH-FLOAT 2.7) can also be expressed as (MAKE-OBJC-INSTANCE 'NS-NUMBER :WITH-FLOAT 2.7)

Previous Section Next Chapter Table of Contents Glossary Index