The problem is that you must use the selector keyword, which returns a SEL, to bind these functions. This is a pointer type, while NSArray stores objects.
Thus, you have three options:
- Use a regular C-type array
- Add the functions to the derived class NSObject, which will call them.
- Use protocol.
The second is probably better, and for this you can use the NSValue class to store selector results. For example;
NSValue* selCommandA = [NSValue valueWithPointer:@selector(handleCommandA:)]; NSValue* selCommandB = [NSValue valueWithPointer:@selector(handleCommandB:)]; NSArray *handler_table = [NSArray arrayWithObjects:selCommandA, selCommandB, nil ];
When you got the correct entry from the array to convert back, you would do:
SEL mySelector = [selCommand pointerValue]; [someObject performSelector:mySelector];
(Note. I assume that from your objective-c syntax, they are intended to be used as methods for an object, not global functions. If you want to use them globally, you should write them like you would C.)
Another option is to formalize the command methods into the protocol. This allows you to write functionality that will work on any object that implements this protocol, and the compiler will provide more verification than if you simply called selectors.
eg.
// some header @protocol CommandHandler @required -(void) handleCommandA; -(void) handleCommandB; @end // some other header @interface someClass : NSObject<CommandHandler> { // you will receive compiler warnings if you do not implement the protocol functions }
Then the processing and sending code is processed to work with objects of the "CommandHandler" type. for example
-(void) registerForCommands:(CommandHandler*)handler
Andrew Grant Feb 22 '09 at 17:53 2009-02-22 17:53
source share