[comp.sys.next] Question about Objective-C's @selector

mdeale@vega.acs.calpoly.edu (Myron Deale) (07/17/90)

I have something like

[listObject makeObjectsPerform:(SEL)aSelector with:anObject];

but can "aSelector" only be a constant?

I have a radio button matrix that inputs a choice between a number of
different methods. I would like to set some kind of pointer (ala C's
pointer to function), based upon the radio button input, though
aSelector would have to be a variable rather than a constant literal.

   The alternative to a pointer is using a bunch of if-then-else's
each with a listObject call. Pretty ugly, but effective. Setting a
flag in the target object and branching inside the called method is
bad software engineering.


-Myron
// mdeale@cosmos.acs.calpoly.edu

wiml@milton.u.washington.edu (William Lewis) (07/17/90)

In article <26a20030.3d07@petunia.CalPoly.EDU> mdeale@vega.acs.calpoly.edu (Myron Deale) writes:
>I have something like
>
>[listObject makeObjectsPerform:(SEL)aSelector with:anObject];
>
>but can "aSelector" only be a constant?
>
>I have a radio button matrix that inputs a choice between a number of
>different methods. I would like to set some kind of pointer (ala C's
>pointer to function), based upon the radio button input, though
>aSelector would have to be a variable rather than a constant literal.

  What's wrong with doing something along the lines of

static SEL selectors[] = { @selector("foo:"), @selector("bar:"), 
                             @selector("doom:") };

...

[listObject makeObjectsPerform:selectors[[sender selectedTag]] with:anObject];
...

   

-- 
wiml@blake.acs.washington.edu       Seattle, Washington  | No sig under
(William Lewis)  |  47 41' 15" N   122 42' 58" W  |||||||| construction

shiffman@sumex-aim.stanford.edu (Smadar Shiffman) (07/17/90)

In article <26a20030.3d07@petunia.CalPoly.EDU> mdeale@vega.acs.calpoly.edu
(Myron Deale) writes:
>I have something like
>
>[listObject makeObjectsPerform:(SEL)aSelector with:anObject];
>
>but can "aSelector" only be a constant?
>
>I have a radio button matrix that inputs a choice between a number of
>different methods. I would like to set some kind of pointer (ala C's
>pointer to function), based upon the radio button input, though
>aSelector would have to be a variable rather than a constant literal.

You could use
[listObject makeObjectsPerform:sel_getUid(radioButtonInput) with:anObject];

where the Objective-C run-time function sel_getUid(radioButtonInput) converts
the string radioButtonInput to a selector at run time.

Hope this helps,
	Smadar
-------

edwardj@microsoft.UUCP (Edward JUNG) (07/17/90)

In article <26a20030.3d07@petunia.CalPoly.EDU> mdeale@vega.acs.calpoly.edu (Myron Deale) writes:
>>I have something like
>>
[stuff deleted for brevity]

You can pass a variable as a SEL parameter.  You can also get the
selector from a string that names the selector (like 'perform:')
using the call "set_getUid()".  Take a look at /usr/include/objc/objc.h
for some helpful APIs for selector hacking.

It isn't clear from your message how you want to get a reference
to a selector...


--
Edward Jung
Systems Architecture
Systems Strategy Task Force
Microsoft Corp.