Need clarification on categories
Need clarification on categories
- Subject: Need clarification on categories
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 19 Mar 2002 10:08:39 -0500
I could use a little help in understanding categories and informal
protocols.
I got the impression from Apple's Objective-C book that a category on an
existing class need not be imported by a client in order to use new methods
declared and implemented in the category, because they are known to the
compiler as full citizens of the existing class. But I encounter a problem:
I create a category on NSString that implements a new class method,
+(id)stringWithBool:. But I get compiler warnings when I call the
stringWithBool: method. For example, [NSString
stringWithBool:someBooleanValue]. The warnings are "cannot find class
(factory) method" and "return type of 'stringWithBool:' defaults to id." The
warnings go away only if I import the category or move the declaration of
the category methods into the client (i.e., make it an informal protocol).
But the application works correctly with or without the import.
Questions:
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?
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?
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.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
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.