Re: Method selectors and parameter passing
Re: Method selectors and parameter passing
- Subject: Re: Method selectors and parameter passing
- From: Mike Ferris <email@hidden>
- Date: Mon, 9 Dec 2002 10:17:12 -0800
Sorry, delivered a copy of this accidentally before I finished...
As a learning experiment I am writing a program that uses Genetic
Algorithms
to modify images. What I would like to do is to define the 'genes' (i
can
never remember what a single element of a sequence is called :o) as
being
commands in a scripting language. So there is a gene for the method
()GaussianBlur, another for ()SwapColorComponents and so on.
In C I would have done this by having each gene contain a function
pointer,
so the parsing and execution of an entire genome would be something
like:
for (index = 0; index < genomeLength; index ++)
{
(genomeList[index])(myImage);
}
In objective C, I assume this kind of thing would be possible with
method
selectors? The problem I have is that I am not sure how to deal with
parameter passing. A function like ()DoGaussianBlur requires one param
for
the radius, something like ()FindEdges doesn't require any, and
something
like ()DrawBox requires many.
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?
Is
there a better way to do this?
There are two ways to go here. One is simple but slightly limited, the
other is general but a bit more complicated.
If the parameters are always objects or ints/chars (ie not floating
point scalars or structs) and you never have more than two parameters,
you can use selectors (and targets unless you always know the object to
send the message to) and then use -performSelector: or
-performSelector:withObject: or -performSelector:withObject:withObject:
(depending on the number of parameters.)
If you need to invoke methods that do not conform to the conditions
above (eg they take floats or structs or have more than two parameters)
then you could store an NSInvocation and use the NSInvocation to invoke
the message.
Mike Ferris
DISCLAIMER: The information contained in this e-mail is confidential
and may
be privileged. It is intended for the addressee only. If you are not
the
intended recipient, please delete this e-mail immediately.
The contents of this email must not be disclosed or copied without the
sender's consent. We cannot accept any resposibility for viruses, so
please
scan all attachments.
The statements and opinions expressed in this message are those of the
author and and do not necessarily reflect those of the company. The
company
does not take any responsibility for the views of the author.
Your secret is safe with us!
_______________________________________________
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.