Oscar Gomez
Oscar Gomez

Reputation: 18488

How to verify if a delegate responds to a selector?

I know I need to write:

 [delegate respondsToSelector:@selector(myMethod:)]

But the compiler is complaining that respondsToSelector is not a method in the protocol, which is correct, However I have seen many sample code use this, how do you do it?

Upvotes: 25

Views: 11167

Answers (2)

James Eichele
James Eichele

Reputation: 119154

Greg Martin has your answer, but here is a quick explanation of why the compiler complains:

The respondsToSelector: method is part of the NSObject protocol, so when you try to send that message to your deleate (of type id), the compiler has no way of knowing that your delegate might be able to handle it.

Upvotes: 9

Greg Martin
Greg Martin

Reputation: 5064

Your @protocol needs to implement <NSObject>, simply update your protocol definition to look like this:

@protocol MyProtocol <NSObject>

Upvotes: 58

Related Questions