All Manuals > LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual > 1 Introduction to the Objective-C Interface > 1.4 Defining Objective-C classes and methods

NextPrevUpTopContentsIndex

1.4.3 Defining Objective-C methods

A class defined with define-objc-class has no methods associated with it by default, other than those inherited from the its ancestor classes. New methods can be defined (or overridden) by using the macros define-objc-method for instance methods and define-objc-class-method for class methods.

Note that the Lisp method definition form is separate from the class definition, unlike in Objective-C where it is embedded in the @implementation block. Also, there is no Lisp equivalent of the @interface block: the methods of an Objective-C class are just those whose defining forms have been evaluated.

When defining a method, various things must be specified:

For example, a method that would be implemented in an Objective-C class as follows:

@implementation MyObject
- (unsigned int)areaOfWidth:(unsigned int)width
                height:(unsigned int)height
{
  return width*height;
}
@end

could be defined in Lisp for instances of the MyObject class from Defining an Objective-C class using the form:

(define-objc-method ("areaOfWidth:height:" (:unsigned :int))
    ((self my-object)
     (width (:unsigned :int))
     (height (:unsigned :int)))
  (* width height))

The variable self is bound to a Lisp object of type my-object and width and height are bound to non-negative integers. The area is returned to the caller as an integer.

1.4.3.1 Special method argument and result conversion

1.4.3.2 Defining a method that returns a structure


LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual - 15 Dec 2011

NextPrevUpTopContentsIndex