• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Method selectors and parameter passing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Method selectors and parameter passing


  • Subject: Re: Method selectors and parameter passing
  • From: Mike Ferris <email@hidden>
  • Date: Mon, 9 Dec 2002 10:13:46 -0800

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

Thanks,
Oli


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.
_______________________________________________
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.
_______________________________________________
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.

  • Follow-Ups:
    • Re: Method selectors and parameter passing
      • From: Mike Ferris <email@hidden>
References: 
 >Method selectors and parameter passing (From: Oliver Donald <email@hidden>)

  • Prev by Date: Re: Hrm... I wonder... Re: "First Run" installation of Application support stuff?
  • Next by Date: Re: Method selectors and parameter passing
  • Previous by thread: Method selectors and parameter passing
  • Next by thread: Re: Method selectors and parameter passing
  • Index(es):
    • Date
    • Thread