Re: Optional methods
Re: Optional methods
- Subject: Re: Optional methods
- From: "Kyle Sluder" <email@hidden>
- Date: Thu, 21 Feb 2008 14:06:53 -0500
On Thu, Feb 21, 2008 at 10:57 AM, Mike R. Manzano <email@hidden> wrote:
> I know about the pattern to shut the compiler up: create a category on
> NSObject with the method defined, but if I were to do that, wouldn't
> respondsToSelector: always return YES for that method, defeating the
> point of calling respondsToSelector: in the first place?
You're not just "shutting the compiler up", you're telling it the
signature of the method you're trying to invoke. If the compiler
hasn't seen the method declaration while it's compiling a compilation
unit, it can't know what arguments it takes or what return type it
gives, so it assumes a signature of the form -(id)methodName:... .
This is why the compiler won't complain about unknown method
signatures if, say, your class has a method -(void)doFoo but you send
doFoo to an object of a distinct type; the compiler just uses the
signature for -doFoo it already knows about, assuming that the one the
receiver implements matches. You get a runtime exception if it
doesn't.
-respondsToSelector: only returns YES for methods that have actually
been implemented. That's why the informal protocol pattern used in
Cocoa works.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden