[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Function as an argument
From: |
Akiko Ogawa - RS/GIS lab |
Subject: |
Re: Function as an argument |
Date: |
Mon, 3 Apr 2000 21:19:35 -0600 (MDT) |
Thank you Marcus,
I definitely like "SEL with perform:" method better. However, I may not be
understanding how to use them correctly. I got an error when I tried.
Tell me if the following example is right:
By the way, Can M() have only (void) functions?
---- In object: EggCarton -----
// This method takes a method as arguments
- goThroughEggsWith: (SEL) methodName InObj: obj {
for (i=0;i<cartonSize;i++) {
id egg = [eggCartonList atOffset: i];
[obj perform: M(methodName) with: egg];
}
...
}
---- In object: EggTester ----
- compareEggTestingMethods {
(id <EggCarton>) eggCarton;
// Here passing methods as arguments
[eggCarton goThroughEggsWith: M(scientificTest:) InObj: self ];
[eggCarton goThroughEggsWith: M(conventionalTest:) InObj: self ];
...
}
// Followings are the passed methods.
- scientificTest: (id) egg {
if ([technitian xRay: egg])
boiledEggs++;
...
}
- conventionalTest: (id) egg {
if ([kid spin: egg])
boiledEggs++;
...
}
>>>>>> "AO" == Akiko Ogawa <- RS/GIS lab <address@hidden>>
writes:
>
>AO> Please advise whether a function can be passed by another function
>AO> as an argument in Objective-C. If yes, what should the syntax be
>AO> like?
>
>Alternatively, you can pass around the SEL type that you get when you
>use M(methodName). Call a SEL with perform:. Functions will be somewhat
>faster.
>
>typedef void (*func_t) (int count, const char **strings);
>
>void
>print_strings (int count, const char **strings)
>{
> int i;
>
> printf ("count: %u\n", count);
>
> for (i = 0; i < count; i++)
> printf ("string[%u]: `%s'\n", i, strings[i]);
>}
>
>void
>call_func (func_t func, int count, const char **strings)
>{
> func (count, strings);
>}
>
>int
>main (int argc, const char **argv)
>{
> call_func (print_strings, argc, argv);
>}
>
>/*
>Local Variables:
>compile-command: "gcc callfunc.c -o callfunc"
>End:
>*/
>
> ==================================
> Swarm-Support is for discussion of the technical details of the day
> to day usage of Swarm. For list administration needs (esp.
> [un]subscribing), please send a message to <address@hidden>
> with "help" in the body of the message.
>
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.