All Manuals > LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual > 2 Objective-C Reference

define-objc-class Macro

Summary

Defines a class and an Objective-C class.

Package

objc

Signature

define-objc-class name (superclass-name*) (slot-specifier*) class-option* => name

Arguments
name
A symbol naming the class to define.
superclass-name
A symbol naming a superclass.
slot-specifier
A slot description as used by cl:defclass.
class-option
A class option as used by cl:defclass.
Values
name
A symbol naming the class to define.
Description

The macro define-objc-class defines a standard-class called name which is used to implement an Objective-C class. Normal cl:defclass inheritance rules apply for slots and Lisp methods.

Each superclass-name argument specifies a direct superclass of the new class, which can be another Objective-C implementation class or any other standard-class, provided that standard-objc-object is included somewhere in the overall class precedence list. The class standard-objc-object is the default superclass if no others are specified.

slot-specifiers are standard cl:defclass slot definitions.

class-options are standard cl:defclass class options. In addition the following options are recognized:

(:objc-class-name objc-class-name)

This option makes the Objective-C class name used for instances of name be the string objc-class-name. If none of the classes in the class precedence list of name have a :objc-class-name option then no Objective-C object is created.

(:objc-superclass-name objc-superclass-name)

This option makes the Objective-C superclass name of the Objective-C class defined by the :objc-class-name option be the string objc-superclass-name. If omitted, the objc-superclass-name defaults to the objc-class-name of the first class in the class precedence list that specifies such a name or to "NSObject" if no such class is found. It is an error to specify a objc-superclass-name which is different from the one that would be inherited from a superclass.

(:objc-instance-vars var-spec*)

This options allows Objective-C instance variables to be defined for this class. Each var-spec should be a list of the form:

(ivar-name ivar-type)

where ivar-name is a string naming the instance variable and ivar-type is an Objective-C FLI type. The class will automatically contain all the instance variables specified by its superclasses.

(:objc-protocols protocol-name*)

This option allows Objective-C formal protocols to be registered as being implemented by the class. Each protocol-name should be a string naming a previously defined formal protocol (see define-objc-protocol). The class will automatically implement all protocols specified by its superclasses.

Notes

If name is not referenced at run time and you deliver an application relying on your class, then you need to arrange for name to be retained during delivery. This can be achieved with the Delivery keyword :keep-symbols (see the Delivery User Guide), but a more modular approach is shown in the example below.

Examples

Suppose your application relies on a class defined like this:

(objc:define-objc-class foo ()
  ()
  (:objc-class-name "Foo"))

If your Lisp code does not actually reference foo at run time then you must take care to retain your class during Delivery. The best way to achieve this is to keep its name on the plist of some other symbol like this:

(setf (get 'make-a-foo 'owner-class) 'foo)

Here make-a-foo is the only code that makes the Foo Objective-C object, so it is the best place to retain the Lisp class foo (that is, only if make-a-foo is retained).

See also

standard-objc-object
define-objc-method
define-objc-class-method
define-objc-protocol
1.4.2 Defining an Objective-C class


LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual - 01 Dec 2021 19:38:32