Re: Need clarification on categories
Re: Need clarification on categories
- Subject: Re: Need clarification on categories
- From: "Erik M. Buck" <email@hidden>
- Date: Tue, 19 Mar 2002 10:37:45 -0600
>
1. Does the rule about not needing to import or declare a category in a
>
client apply only when the category reimplements a method that is already
>
declared in the base class?
It is necessary to provide a declaration of any method used if you don't
want the compiler to issue a warning. When creating a category of NSString
that adds new public methods, you should do it in a separate file and
provide a .h file to import with the method declarations. Any code that
uses the added public methods can import the header for the category and
silence the compiler. The extra methods are present and work regardless
whether you import the header. The header just avoids warnings. Using the
id type may also avoid warnings.
>
>
2. Is the choice between declaring the category method in a file of its
own
>
as opposed to declaring it in the client file simply a matter of whether I
>
want to use the category method in one client as opposed to multiple
>
clients?
I like to always declare category methods in a file of their own so that
when I use Autodoc I get nice documentation for the category. The answer to
your question is yes apart from aesthetic concerns.
>
>
3. How do I use a category method that I have added to an existing
abstract
>
class like NSControl? I want every control to respond to my new method. I
>
write a category on NSControl that declares and implements the new method.
>
But when I go to place a control, say, a text field, in my application,
how
>
do I get the text field to respond to the new method? Typing my control as
>
an NSTextField won't work, because I don't have access to the NSTextField
>
source and can't add an "#import "myCategory.h" line to it. Do I have to
>
subclass NSTextField and import myCategory in the subclass? But then
what's
>
the point of a category? -- I could just write the method in my
NSTextField
>
subclass in the first place.
>
>
I have the feeling that I'm overlooking something fundamental.
>
I think you have overlooked something fundamental also because I can't
understand your question.
When methods are added to a class via a category, the methods are just like
methods added in the original implementation. They are inherited, etc. If
you add a method to the NSControl class, all sub-classes of NSControl
inherit that method. If you add a method to NSObject via a category, all
subclasses of NSObject inherit the method.
If you are just asking the following:
"I added an action method to NSControl via a category, but when I draw
connection lines to controls in Interface Builder, the new action is not
shown. How do I get IB to show the added action ?"
Just drag the header file for your category of NSControl into IB to tell it
about the methods.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.