Professional Web Applications Themes

Can one pass variables through a selector? - Mac Programming

In article <clund-50F2A4.12443709092003amstwist00.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 ...

  1. #1

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003amstwist00.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. #2

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.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

  3. #3

    Default Re: Can one pass variables through a selector?

    C Lund <clundNOSPAMnotam02.no> wrote in message news:<clund-50F2A4.12443709092003amstwist00.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

  4. #4

    Default Re: Can one pass variables through a selector?

    In article <clund-50F2A4.12443709092003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.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

  5. #5

    Default Re: Can one pass variables through a selector?

    In article <mail-6B537B.23515709092003localhost>,
    Michael Ash <mailmikeash.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

  6. #6

    Default Re: Can one pass variables through a selector?

    C Lund <clundNOSPAMnotam02.no> wrote in message news:<clund-017CFA.10003810092003amstwist00.chello.com>...
    > In article <mail-6B537B.23515709092003localhost>,
    > Michael Ash <mailmikeash.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

Similar Threads

  1. CF pass variables to Flash
    By lvbao in forum Coldfusion Flash Integration
    Replies: 4
    Last Post: May 8th, 10:26 PM
  2. trying to pass two variables to a detail page??
    By AS Dawg in forum Dreamweaver AppDev
    Replies: 1
    Last Post: April 7th, 07:39 PM
  3. Insert Record and pass url variables
    By brianevans5179 in forum Dreamweaver AppDev
    Replies: 6
    Last Post: March 30th, 01:46 PM
  4. Interesting Pass Variables to Flash
    By DefaultDave in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 17th, 09:49 PM
  5. IS it better to use session variables or pass a URL variable
    By George Mizzell in forum Macromedia Dreamweaver
    Replies: 2
    Last Post: July 11th, 07:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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