Re: Method selectors and parameter passing
Re: Method selectors and parameter passing
- Subject: Re: Method selectors and parameter passing
- From: Simon Stapleton <email@hidden>
- Date: Tue, 10 Dec 2002 14:21:53 +0100
Oliver Donald <email@hidden> wrote:
Basically I need to know how I can store NSArray's of selectors + their
parameters for variable-lenght parameter lists . Can anyone provide
pointers
(no pun intended) to how I could implement the above C in Objective-C?
Using class_getInstanceMethod(), you can get an objc_method structure
for a given selector in a given class, which would be one way to do it.
See
file://localhost/Developer/Documentation/Cocoa/ObjectiveC/
9objc_runtime_reference/index.html for more details, with specific
reference to the 'Accessing Methods' section.
However, when it comes time to call the method, you're going to end up
doing a lot of tedious checking of argument types, before calling the
method itself. This is liable to be a pain.
Is there a better way to do this?
I'm sure there is, but it very much depends on your implementation.
The simplest way to deal with this is to make sure each method has the
same signature, while allowing for multiple parameters. To do that :
wrap up the parameters themselves in some sort of collection: either
an NSArray or an NSDictionary - an NSArray would allow you complete
freedom in terms of what you pass in, and an NSDictionary would allow
you more control over the types being passed in.
If we assume that your 'genome' returns a pipeline, and that we're
working on NSImages, and that the selectors will be added as a category
on NSImage, and we want to use named parameters, the signature for each
selector might look something like this:
- (NSImage*) imageByPerformingXWithParameters: (NSDictionary*)
parameters;
where X is replaced by the name of each 'command' and the
implementation of each of these does any necessary parameter checking.
Then you might use the following code to evaluate a genome (assuming
the genome is wrapped as an NSArray of NSArrays, with each subarray
containing a SEL and a dictionary (possibly empty) of parameters):
NSImage * theImage = .....;
id enumerator = [genome objectEnumerator];
id gene;
while (gene = [enumerator nextObject]) {
SEL selector = [gene objectAtIndex:0];
id params = [gene objectAtIndex:0];
theImage = [theImage performSelector:selector withObject:params];
}
This is, of course, only one of a multitude of possible solutions. To
find the best one, you might try using GAs ;-)
Simon
--
AppleCare. Now _there's_ an oxymoron for you.
--
PGP Key Id : 0x50D0698D
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.