|Version 13 (modified by mikel, 8 years ago) (diff)|
Writing the Lisp Code
In this section we'll write Lisp code that duplicates the features provided by the Objective C code in Apple's tutorial. In Apple's tutorial, the explanation of the Objective C code begins with the section Bridging the Model and View: The Controller.
The Lisp code in this section of the HOWTO is considerably simpler than the corresponding Objective C code, in part because we can ignore the conventions that XCode uses for laying out source files. We can just write all our definitions into a single Lisp source file, and load that file into Clozure CL when we are ready to build the application. The Lisp code is also a little more compact than the corresponding Objective-C.
First Things First
Place the following line at the top of your Lisp source file:
OpenMCL's Objective-C bridge code is defined in the "CCL" package. Usually, when building an application, you'll create a package for that application and import the definitions you need to use. In order to keep the discussion short in this simple example, we just place all our definitions in the "CCL" package.
Defining the Converter Class
We begin by defining the Converter class. Recall from Apple's tutorial that this is the Model class that implements the conversion between dollars and other currencies. Here is the Lisp definition that implements the class you created in InterfaceBuilder:
(defclass converter (ns:ns-object) () (:metaclass ns:+ns-object))
This is an ordinary CLOS class definition, with a couple of simple wrinkles. First, the superclass it inherits from is the NS-OBJECT class in the "NS" package. NS-OBJECT is an Objective-C class, the ancestor of all Objective-C objects. This CLOS definition creates a new Objective-C class named "Converter".
We tell OpenMCL how to build the right kind of class object by including the :METACLASS option in the definition:
The Objective-C bridge knows that when the metaclass is ns:+ns-object, it must lay out the class object in memory as an Objective C class, rather than a normal CLOS STANDARD-CLASS.
Next, we define the method "convertCurrency:atRate:":
(objc:defmethod (#/convertCurrency:atRate: :float) ((self converter) (currency :float) (rate :float)) (* currency rate))
This is the method that actually does the currency conversion. It's very simple--really, it just multiples currency times rate. Most of the text in the definition is Objective C bridge code that links the definition to the right class with the right argument and return types.
objc:defmethod is a version of DEFMETHOD that creates Objective C method definitions.
The syntax #/convertCurrency:atRate: uses the "#/" reader macro to read a symbol with case preserved, so that you can see in your code the same name that Objective-C uses for the method, without worrying about how the name might be converted between Lisp and Objective-C naming conventions.
The number of arguments to an Objective-C method is the number of colons in the name, plus one. Each colon indicates an argument, and there is always an extra "self" argument that refers to the object that receives the message. The "self" argument is implicit in Objective-C definitions, but it's just another argument in the Lisp code.
We indicate the return type and the types of arguments in the method definition by surrounding parameters and the method name with parentheses, and appending the type name. Thus, for example,
means that the return type of the method is :FLOAT, and
means that the type of the object that receives the convertCurrency:atRate: message is Converter.
You will see these same conventions repeated in the next section.