NextPrevUpTopContentsIndex

1.4.2 Defining an Objective-C class

An Objective-C class implemented in Lisp and its associated subclass of standard-objc-object should be defined using the macro define-objc-class. This has a syntax similar to defclass, with additional class options including :objc-class-name to specify the name of the Objective-C class.

If the superclass list is empty, then standard-objc-object is used as the default superclass, otherwise standard-objc-object must be somewhere on class precedence list or included explicitly.

For example, the following form defines a Lisp class called my-object and an associated Objective-C class called MyObject .

(define-objc-class my-object ()
  ((slot1 :initarg :slot1 :initform nil))
  (:objc-class-name "MyObject"))

The class my-object will inherit from standard-objc-object and the class MyObject will inherit from NSObject . See How inheritance works for more details on inheritance.

The class returned by (find-class 'my-object) is associated with the Objective-C class object for MyObject , so

(objc-object-pointer (find-class 'my-object))

and

(coerce-to-objc-class "MyObject")

will return a pointer to the same foreign object.

When an instance of my-object is made using make-instance , an associated foreign Objective-C object of the class MyObject is allocated by calling the class's "alloc" method and initialized by calling the instance's "init" method. The :init-function initarg can be used to call a different initialization method.

Conversely, if the "allocWithZone:" method is called for the class MyObject (or a method such as "alloc" that calls "allocWithZone:" ), then an associated object of type my-object is made.


LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual - 4 Apr 2005

NextPrevUpTopContentsIndex