Re: Need clarification on categories
Re: Need clarification on categories
- Subject: Re: Need clarification on categories
- From: Ondra Cada <email@hidden>
- Date: Tue, 19 Mar 2002 17:15:06 +0100
On Tuesday, March 19, 2002, at 04:08 , Bill Cheeseman wrote:
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.
Right. As soon as category is linked in (be it statically or dynamically),
it works.
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.
Right. Casting, preprocessor, and extern object delarations aside, headers
are just for your conveniency, without them ALL WOULD WORK PRECISELY THE
SAME WAY.
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?
No, it does not. It applies generally.
That does not mean you should not import the header: you should, to make
compiler know the new methods, and be able thus to warn you if you do some
typo or so. It is not needed for the category to work, though.
You know plain C? If so, consider function prototypes: regardless you use
them or not, functions will be called properly (casting aside, again). The
@interfaces work the same way as function prototypes.
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?
More or less, yup. IMHO a good programming style would be to use an extra
header anyway, but others might differ in opinion. The important thing is
that REGARDLESS YOU DO IT, the category would work just as well.
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.
right.
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?
Just by linking-in the category, that's all.
So as the compiler can check typos etc., you also MIGHT (but don't need to)
import the appropriate header.
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.
It would work nicely, since there is utterly *no* reason why there should
be "#import "myCategory.h" line in the NSTextField source. Don't please
confuse Objective C with the C++ crap!
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.
Right. Should you have to recompile the original class to make a category
working, there would indeed be no point of categories. Luckily, Objective
C is a decent dynamic language which means you *don't* have to do that --
just link them both in is quite sufficient.
I have the feeling that I'm overlooking something fundamental.
Yup, like many others, the dynamic OO system ;)
You don't need an access to TextEdit sources. Just link-in the original
class (in this case it's in AppKit framework) *and* the category of it,
created independently (in this case, it would be in your app code, but as
well it could be in a different framework or loadable bundle). That's all,
it works.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.