Question about respondsToSelector

  • Does the id type have enough information for the respondsToSelector
    method to work. I have a class with an ivar of type id, and when I
    invoke the respondsToSelector method it fails when it should succeed.
    I am assuming it should work fine, because if I skip checking with the
    respondsToSelector method and just make the call, it executes the
    method. Can someone please tell me what I am doing wrong?

    Thanks,
    Carmen
  • On Aug 18, 2008, at 1:17 PM, Carmen Cerino Jr. wrote:

    > Does the id type have enough information for the respondsToSelector
    > method to work. I have a class with an ivar of type id, and when I
    > invoke the respondsToSelector method it fails when it should
    > succeed. I am assuming it should work fine, because if I skip
    > checking with the respondsToSelector method and just make the call,
    > it executes the method. Can someone please tell me what I am doing
    > wrong?

    It should work just fine.  This example...

    NSArray* theArray = [NSArray array];
    id theId = theArray;
    id<NSCopying>* theCopyingId = (id<NSCopying>*) theArray;

    if ([theArray respondsToSelector:@selector(isEqualToArray:)]) {
        NSLog(@"theArray responds to isEqualToArray:");
    } else {
        NSLog(@"theArray DOES NOT respond to isEqualToArray:");
    }

    if ([theId respondsToSelector:@selector(isEqualToArray:)]) {
        NSLog(@"theId responds to isEqualToArray:");
    } else {
        NSLog(@"theId DOES NOT respond to isEqualToArray:");
    }

    if ([(id) theCopyingId respondsToSelector:@selector(isEqualToArray:)]) {
        NSLog(@"theCopyingId responds to isEqualToArray:");
    } else {
        NSLog(@"theCopyingId DOES NOT respond to isEqualToArray:");
    }

    ...displays this output:

    theArray responds to isEqualToArray:
    theId responds to isEqualToArray:
    theCopyingId responds to isEqualToArray:

    Note that special casting done to remove warnings.  However, without
    the casting, output was the same.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • On Aug 18, 2008, at 11:17 AM, Carmen Cerino Jr. wrote:

    > Does the id type have enough information for the respondsToSelector
    > method to work. I have a class with an ivar of type id, and when I
    > invoke the respondsToSelector method it fails when it should
    > succeed. I am assuming it should work fine, because if I skip
    > checking with the respondsToSelector method and just make the call,
    > it executes the method. Can someone please tell me what I am doing
    > wrong?

    What code are you using with -respondsToSelector: and to invoke the
    selector?

    We need to see that in order to be able to say whether what you're
    doing will work.

    On background:  -respondsToSelector: is a message like any other
    message in Objective-C.  It's not an operation on a type; you're
    actually sending a message to the object in question, which will use
    the Objective-C runtime to answer the question that's asked.  So the
    static type of the variable referencing the object it's sent to -- in
    your case, "id" -- doesn't have anything to do with how the actual
    message behaves.

      -- Chris
  • On Aug 18, 2008, at 11:17 AM, Carmen Cerino Jr. wrote:

    > Does the id type have enough information for the respondsToSelector
    > method to work. I have a class with an ivar of type id, and when I
    > invoke the respondsToSelector method it fails when it should
    > succeed. I am assuming it should work fine, because if I skip
    > checking with the respondsToSelector method and just make the call,
    > it executes the method. Can someone please tell me what I am doing
    > wrong?

    Perhaps you've misspelled the name of the selector - just like you've
    done above with "respondsToSelector:" (note the trailing ":").

    j o a r
  • On Aug 18, 2008, at 2:17 PM, Carmen Cerino Jr. wrote:
    > Does the id type have enough information for the respondsToSelector
    > method to work.

    It's not whether the id type has information.  It's whether the object
    itself is able to respond to a message, and that depends on what class
    the object is an instance of.

    > I have a class with an ivar of type id, and when I invoke the
    > respondsToSelector method it fails when it should succeed.

    When you have a problem, try to explain what you expected to happen
    and what actually happened.  What results would have constituted
    success?  What actually happened that you are calling failure?  A
    crash?  An error message?  A value displayed that you weren't
    expecting?  What object were you sending the respondsToSelector
    message to?  What selector were you passing?  What was the actual
    object in the id variable at the time?

    > I am assuming it should work fine, because if I skip checking with
    > the respondsToSelector method and just make the call, it executes
    > the method. Can someone please tell me what I am doing wrong?

    It would be help if you post your code and describe what you are
    trying to do.

    --Andy
  • Also: note that method names are case-sensitive, so
    @selector(doSomething) is not the same as @selector(doSomeThing).

    Also, if the method you are referring to takes an argument, make sure
    you aren't forgetting the colon at the end.  The name of the method
    contains all its colons; @selector(doSomething) is different from
    @selector(doSomething:).

    --Andy
  • Am Mo,18.08.2008 um 20:17 schrieb Carmen Cerino Jr.:

    > Does the id type have enough information for the respondsToSelector
    > method to work. I have a class with an ivar of type id, and when I
    > invoke the respondsToSelector method it fails when it should
    > succeed. I am assuming it should work fine, because if I skip
    > checking with the respondsToSelector method and just make the call,
    > it executes the method. Can someone please tell me what I am doing
    > wrong?
    Since the whole dispatch is done by and only by the runtime
    (exception: messages to super) the rte holds the information. (The
    compiler only throws warnings, because he can proceed with his work.
    This is, because he does nothing else than checking!)

    As others said:
    a) We need a piece of code
    b) Typo?

    Cheers,
    Amin

    >
    >
    > Thanks,
    > Carmen

    Amin Negm-Awad
    <negm-awad...>
  • Sorry about the sketchy details. Basically I have a wrapper class for
    the Sequence Grabber, and I want to setup a delegate for the
    decompression callback.

    This is where I use the respondsToSelector method:
    static void SGVideoDecompTrackingCallback(
      void *decompressionTrackingRefCon,
      OSStatus result,
      ICMDecompressionTrackingFlags decompressionTrackingFlags,
      CVPixelBufferRef pixelBuffer,
      TimeValue64 displayTime,
      TimeValue64 displayDuration,
      ICMValidTimeFlags validTimeFlags,
      void *reserved,
      void *sourceFrameRefCon )
    {
    #pragma unused(reserved)
    #pragma unused(sourceFrameRefCon)

    SGVideo* sgVideoChan = (SGVideo*)decompressionTrackingRefCon;
    id _delegate = sgVideoChan.delegate;

    if (result == noErr){

            if ([_delegate respondsToSelector:
    @selector(SGDecompDataProc:)]){

                [_delegate SGDecompDataProc:pixelBuffer
                  trackingFlags:decompressionTrackingFlags
                  displayTime:displayTime
                  displayDuration:displayDuration
                  validTimeFlags:validTimeFlags];
            }
        }
    }

    Header file where the function I am checking is prototyped:

    @interface VideoController : NSObject {
    SeqGrab* mSeqGrab;
    SGVideo* mVideoChan;
    IBOutlet SampleCIView* mPreview;
    }

    -(void)SGDecompDataProc: (CVPixelBufferRef)pixelBuffer trackingFlags:
    (ICMDecompressionTrackingFlags)decompressionTrackingFlags displayTime:
    (TimeValue64)displayTime displayDuration:(TimeValue64)displayDuration
    validTimeFlags:(ICMValidTimeFlags)validTimeFlags;

    @end

    Please let me know if you need to see anything else. Once again I
    apologize for the sketchy details.

    Thank you all for your help!

    Carmen

    On Aug 18, 2008, at 4:39 PM, Andy Lee wrote:

    > Also: note that method names are case-sensitive, so
    > @selector(doSomething) is not the same as @selector(doSomeThing).
    >
    > Also, if the method you are referring to takes an argument, make
    > sure you aren't forgetting the colon at the end.  The name of the
    > method contains all its colons; @selector(doSomething) is different
    > from @selector(doSomething:).
    >
    > --Andy
    >
  • On Tue, Aug 19, 2008 at 2:33 PM, Carmen Cerino Jr. <ccerinojr...> wrote:
    > Sorry about the sketchy details. Basically I have a wrapper class for the
    > Sequence Grabber, and I want to setup a delegate for the decompression
    > callback.
    >
    > This is where I use the respondsToSelector method:
    > static void SGVideoDecompTrackingCallback(
    > void *decompressionTrackingRefCon,
    > OSStatus result,
    > ICMDecompressionTrackingFlags decompressionTrackingFlags,
    > CVPixelBufferRef pixelBuffer,
    > TimeValue64 displayTime,
    > TimeValue64 displayDuration,
    > ICMValidTimeFlags validTimeFlags,
    > void *reserved,
    > void *sourceFrameRefCon )
    > {
    > #pragma unused(reserved)
    > #pragma unused(sourceFrameRefCon)
    >
    > SGVideo* sgVideoChan = (SGVideo*)decompressionTrackingRefCon;
    > id _delegate = sgVideoChan.delegate;
    >
    > if (result == noErr){
    >
    > if ([_delegate respondsToSelector: @selector(SGDecompDataProc:)]){
    >
    > [_delegate SGDecompDataProc:pixelBuffer
    > trackingFlags:decompressionTrackingFlags
    > displayTime:displayTime
    > displayDuration:displayDuration
    > validTimeFlags:validTimeFlags];
    > }
    > }
    > }
    >
    >
    > Header file where the function I am checking is prototyped:
    >
    > @interface VideoController : NSObject {
    > SeqGrab* mSeqGrab;
    > SGVideo* mVideoChan;
    > IBOutlet SampleCIView* mPreview;
    > }
    >
    > -(void)SGDecompDataProc: (CVPixelBufferRef)pixelBuffer
    > trackingFlags:(ICMDecompressionTrackingFlags)decompressionTrackingFlags
    > displayTime:(TimeValue64)displayTime
    > displayDuration:(TimeValue64)displayDuration
    > validTimeFlags:(ICMValidTimeFlags)validTimeFlags;
    >
    >
    > @end
    >
    > Please let me know if you need to see anything else. Once again I apologize
    > for the sketchy details.
    >
    > Thank you all for your help!

    Heya - that's not the entire selector.  You want to use :

    if ([_delegate respondsToSelector: @selector(SGDecompDataProc:
    trackingFlags: displayTime: displayDuration: validTimeFlags:)]) {
    ...
    }
  • *Smacks forehead*

    Believe it or not I had that originally and it didn't work. I must of
    had a type-o in the function header that I was passing to
    respondsToSelector.

    Thank you all. I am going to go back to making an ass out of myself in
    the privacy of my own home.

    Cheers,
    Carmen

    On Aug 19, 2008, at 9:55 AM, Jonathan del Strother wrote:

    > GDecompDataProc:
    > trackingFlags: displayTime: displayDuration: validTimeFlags:
  • Am 19.08.2008 um 15:36 schrieb "Carmen Cerino Jr."
    <ccerinojr...>:

    > if ([_delegate respondsToSelector:
    > @selector(SGDecompDataProc:)]){

    The signature of your method seems to be:

    SGDecompDataProc:trackingFlags:displayTime:displayDuration:validTimeFlag
    s:

    not:

    SGDecompDataProc:

    So you are testing for the wrong method here.

    >
    > [_delegate SGDecompDataProc:pixelBuffer
    > trackingFlags:decompressionTrackingFlags
    > displayTime:displayTime
    > displayDuration:displayDuration
    > validTimeFlags:validTimeFlags];

    HTH
    Mike
    --
    Mike Fischer    Softwareentwicklung, EDV-Beratung
                                    Schulung, Vertrieb
    Note:            I read this list in digest mode!
          Send me a private copy for faster responses.
  • On Aug 19, 2008, at 6:33 AM, Carmen Cerino Jr. wrote:

    >
    > if ([_delegate respondsToSelector:
    > @selector(SGDecompDataProc:)]){
    >
    > [_delegate SGDecompDataProc:pixelBuffer
    > trackingFlags:decompressionTrackingFlags
    > displayTime:displayTime
    > displayDuration:displayDuration
    > validTimeFlags:validTimeFlags]

    > [had full selector originally, but didn't work]

    > *Smacks forehead*
    >
    > Believe it or not I had that originally and it didn't work. I must
    > of had a type-o in the function header that I was passing to
    > respondsToSelector.

    One way to avoid that sort of problem is the "ifResponds" Higher Order
    Message:

    [[_delegate ifResponds] SGDecompDataProc:pixelBuffer
                  trackingFlags:decompressionTrackingFlags
                  displayTime:displayTime
                  displayDuration:displayDuration
                  validTimeFlags:validTimeFlags]

    This will send the message if _delegate responds to it.

    Cheers,

    Marcel
previous month august 2008 next month
MTWTFSS
        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
Go to today