All Manuals > LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual > 1 Introduction to the Objective-C Interface > 1.3 Invoking Objective-C methods

NextPrevUpTopContentsIndex

1.3.7 Invoking a method that returns values by reference

Values are returned by reference in Objective-C by passing a pointer to memory where the result should be stored, just like in the C language. The Objective-C interface in Lisp works similarly, using the standard FLI constructs for this.

For example, an Objective-C method declared as

- (void)getValueInto:(int *)result;

might called from Objective-C like this:

int getResult(MyObject *object)
{
  int result;
  [object getValueInto:&result];
  return result;
}

The equivalent call from Lisp can be made like this:

(defun get-result (object)
  (fli:with-dynamic-foreign-objects ((result-value :int))
    (objc:invoke object "getValueInto:" result-value)
    (fli:dereference result-value)))

The same technique applies to in/out arguments, but adding code to initialize the dynamic foreign object before calling the method.


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

NextPrevUpTopContentsIndex