Edit AllPages

An implentation of a TrampolineObject as described in HigherOrderMessaging. Notes follow below.


#import <Foundation/Foundation.h>

//It’d probably be nicer to use an enum here #define kDoMode 0 #define kCollectMode 1 #define kSelectMode 2 #define kRejectMode 3

@interface BSTrampoline : NSProxy { NSEnumerator *enumerator; int mode; NSArray *temp; //For returning from collect, select, reject }


#import “BSTrampoline.h”

@implementation BSTrampoline


Feel free to use this code for anything you want, although it’d be nice if you credited the original author (ThomasCastiglione).

BSTrampoline differs slightly from RobRix’s description of a trampoline in that it makes use of NSEnumerator, making the implementation of categories on collection objects to use it trivial. For example, categories on NSArray:

#import <Foundation/Foundation.h> #import “BSTrampoline.h”

@interface NSArray(HigherOrderMessaging)


@implementation NSArray(HigherOrderMessaging)


If anybody has any suggestions for ways to improve the code or finds bugs in it, please say so. A simple test rig can be found at BSTrampolineTestRig - it will work with the provided code if NSArray(HigherOrderMessaging) is split into header and implementation files.

Regarding undocumented APIs: BSTrampoline makes use of two method calls for which Apple provides no documentation or public headers. Specifically, NSMethodSignature + signatureWithObjCTypes and NSInvocation -initWithMethodSignature. The former is necessary to create method signatures at all, and the latter to reuse a forwarded invocation, fooling the calling code into accepting a different return value. Both of these methods are documented in the OpenStep and GNUStep specifications, and hopefully at some point Apple will also follow this path.

Finally, the formatting of encoded method signatures is defined at . This is technically implementation-dependant, although all current objective-c runtimes use the same formatting. For greater portability, “@^v^c” could, for example, be replaced with “@encode(id ) @encode(void) @encode(char)”.