34

I've implemented a protocol with an optional method and in the calling method I want to send respondsToSelector: to self.delegate before I send the message, but that does not compile. The fail message is:

No Known instance method for selector 'respondsToSelector'.

As a work-around, I "sanitized" the delegate as shown below, which compiles...

//MyClass.h:

@class MyClass;

@Protocol MyClassDelegate
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
@end

@interface MyClass : UIViewController

@property (nonatomic, weak) id<MyClassDelegate> delegate;

@end

and

//MyClass.m:

...
@synthesize delegate = _delegate;
...

id sanitizedDelegate = self.delegate; //Hmmmm... why does this work?

if ([sanitizedDelegate respondsToSelector:@selector(myClass:willDoSomething:)]) {
    [self.delegate myClass:self willDoSomething:animated];
}

.

I checked a number of posts including this one but it does not answer the compilation fail issue.

Also, alternative accessors do not work...

[self delegate]
//or
_delegate

Has anyone seen this or can advise a better way of handling?

IOS 5.0:(9A334), Xcode 4.2.1 (4D502)

Community
  • 1
  • 1
vmanjz
  • 1,156
  • 1
  • 10
  • 23

2 Answers2

104

-respondsToSelector: is a method on NSObject. Either assume that your id delegate is in fact an NSObject, and cast it:

[(NSObject*)self.delegate respondsToSelector:@selector(myClass:willDoSomething:)]

Or, better, make your delegate explicitly an NSObject:

@property (nonatomic, weak) NSObject<MyClassDelegate>* delegate;

Or make the protocol be a sub-protocol of NSObject:

@protocol MyClassDelegate <NSObject>

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
15

Basically you are saying that your delegate is constrained only by your <MyClassDelegate> protocol so the compiler assumes that those are the only methods available. What you need to do is have the protocol extend <NSObject> like so:

@Protocol MyClassDelegate <NSObject>
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
@end

That way the compiler knows that any object which conforms to your protocol also conforms to the <NSObject> protocol which defines respondsToSelector:.

NJones
  • 27,139
  • 8
  • 70
  • 88