Can one pass variables through a selector?

Ask a Question related to Mac Programming, Design and Development.

  1. #1

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003@amstwist00.chello.com>, C Lund wrote:
    > Assuming the following declarations:
    >
    > SEL selectorName;
    >
    > -(void)aCoordi:(int)iCoord j:(int)jCoord;
    > -(void)bCoordi:(int)iCoord j:(int)jCoord;
    > -(void)cCoordi:(int)iCoord j:(int)jCoord;
    >
    > And the above are called by:
    >
    > [self performSelector:selectorName];
    >
    > Is there any way to pass iCoord and jCoord through the selector or
    > would I have to make iCoord and jCoord global (within the class)? I
    > suspect the latter, but I'm asking just in case it can be done.
    Yes - either use an NSInvocation to perform the call, or call objc_msgSend()
    directly. NSInvocation is the 'official' way to do it, and is mentioned
    in the docs for performSelector:.

    Andrew.
    Andrew Hunter Guest

  2. Similar Questions and Discussions

    1. CF pass variables to Flash
      Hello: Basically I want Coldfusion to pass the server date to Flash. But I get '#theDt#' instead of the value of 'theDt' please see the code...
    2. Interesting Pass Variables to Flash
      What I am attempting to do in a whole is to create swf files that will be drawn from several premaid swf's, alls I want to do is change the data...
    3. Postfields in WML does not pass all variables to next WML page
      I am writing a function as listed below. My problem is that this form passes only the variable 'state' and not the others. Can somebody help me? ...
    4. pass variables when execute a Dir movie
      Hello, I'd like to know if it's possible to initialize a Director movie from othe application (for example C++Builder) passing it already some...
    5. IS it better to use session variables or pass a URL variable
      Yes, I am sure the answer starts with - It depends... Well, I am trying to get a person's name from one page to the next. The name will obviously...
  3. #2

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003@amstwist00.chello.com>,
    C Lund <clund@NOSPAMnotam02.no> wrote:
    > Is there any way to pass iCoord and jCoord through the selector or
    > would I have to make iCoord and jCoord global (within the class)? I
    > suspect the latter, but I'm asking just in case it can be done.
    Look at NSInvocation instead.
    Doc O'Leary Guest

  4. #3

    Default Re: Can one pass variables through a selector?

    C Lund <clund@NOSPAMnotam02.no> wrote in message news:<clund-50F2A4.12443709092003@amstwist00.chello.com>...
    > Assuming the following declarations:
    >
    > SEL selectorName;
    >
    > -(void)aCoordi:(int)iCoord j:(int)jCoord;
    > -(void)bCoordi:(int)iCoord j:(int)jCoord;
    > -(void)cCoordi:(int)iCoord j:(int)jCoord;
    >
    > And the above are called by:
    >
    > [self performSelector:selectorName];
    >
    > Is there any way to pass iCoord and jCoord through the selector or
    > would I have to make iCoord and jCoord global (within the class)? I
    > suspect the latter, but I'm asking just in case it can be done.
    Take a look at NSInvocation. Basicly you need to follow these steps:

    1) Convert your SEL into NSMethodSignature using -methodSignatureForSelector:
    2) Create an NSInvocation object using that method signature
    3) Set the arguments in the invocation object using -setArgument:atIndex:
    4) Send invokeWithTarget: to the invocation object

    Michael
    Michael Milvich Guest

  5. #4

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003@amstwist00.chello.com>,
    C Lund <clund@NOSPAMnotam02.no> wrote:
    > Assuming the following declarations:
    >
    > SEL selectorName;
    >
    > -(void)aCoordi:(int)iCoord j:(int)jCoord;
    > -(void)bCoordi:(int)iCoord j:(int)jCoord;
    > -(void)cCoordi:(int)iCoord j:(int)jCoord;
    >
    > And the above are called by:
    >
    > [self performSelector:selectorName];
    >
    > Is there any way to pass iCoord and jCoord through the selector or
    > would I have to make iCoord and jCoord global (within the class)? I
    > suspect the latter, but I'm asking just in case it can be done.
    You can just do [object performSelector:@selector(aCoordi:j:)
    withObject:iCoord withObject:jCoord]. This is a hack, because you're
    relying on the fact that you can treat integers as objects if you don't
    send messages to them, in current incarnations of Mac OS X. This will
    break if you compile for a hypothetical 64-bit version of OS X which
    would have 64-bit pointers. PerfomSelector:withObject:withObject:
    doesn't send messages, it just passes them along, but that's also
    somewhat dangerous to rely on.

    Otherwise, you can simply do objc_msgSend(object, @selector(aCoordi:j:),
    iCoord, jCoord). Whevere you send a message with [] it gets pretty much
    directly translated to a call to objc_msgSend() anyway.

    IMO using NSInvocation for this is overkill.
    Michael Ash Guest

  6. #5

    Default Re: Can one pass variables through a selector?

    In article <mail-6B537B.23515709092003@localhost>,
    Michael Ash <mail@mikeash.com> wrote:
    > You can just do [object performSelector:@selector(aCoordi:j:)
    > withObject:iCoord withObject:jCoord]. This is a hack, because you're
    > relying on the fact that you can treat integers as objects if you don't
    > send messages to them, in current incarnations of Mac OS X. This will
    > break if you compile for a hypothetical 64-bit version of OS X which
    > would have 64-bit pointers.
    Wouldn't a recompile for a 64-bit OS fix it again?

    BTW: Thanks for all the answers to my question. B)

    --
    C Lund, [url]www.notam02.no/~clund[/url]
    C Lund Guest

  7. #6

    Default Re: Can one pass variables through a selector?

    C Lund <clund@NOSPAMnotam02.no> wrote in message news:<clund-017CFA.10003810092003@amstwist00.chello.com>...
    > In article <mail-6B537B.23515709092003@localhost>,
    > Michael Ash <mail@mikeash.com> wrote:
    >
    > > You can just do [object performSelector:@selector(aCoordi:j:)
    > > withObject:iCoord withObject:jCoord]. This is a hack, because you're
    > > relying on the fact that you can treat integers as objects if you don't
    > > send messages to them, in current incarnations of Mac OS X. This will
    > > break if you compile for a hypothetical 64-bit version of OS X which
    > > would have 64-bit pointers.
    >
    > Wouldn't a recompile for a 64-bit OS fix it again?
    No it wouldn't. Its a hack because a 32 bit pointer looks just like a
    32 bit integer. As long as you don't dereference the pointer everyone
    is happy. When you recompile for 64-bit it will create a stack frame
    with two 64 bit pointers or 128 bits worth of data, where your method
    was only expecting two 32 bit ints or 64 bits of data. I suppose you
    could change everything to longs and pass around 64 bit integers, but
    that is weak.

    Michael
    Michael Milvich Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139