Re: Question about categories
Re: Question about categories
- Subject: Re: Question about categories
- From: Bill Bumgarner <email@hidden>
- Date: Sat, 2 Sep 2006 22:10:16 -0700
On Sep 2, 2006, at 3:19 PM, Alan Smith wrote:
So, sinse I can't add onto a method... If I subclass it there are
several methods that return an object of its class. That wouldn't do
because then only the first object would have the extra methods that I
add. So... I tried overriding those methods like this:
I know you don't want to hear it, but I am going to suggest that you
read a particular document. In particular, start with:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
index.html
Read it twice. I have been writing Obj-C code since 1989 and I still
learn things from reading that document (often because Apple or NeXT
changed it to reflect the current patterns of choice).
A category adds methods to a class. All instances of that class
(and all subclasses of that class) will have those additional methods.
Now, a category can also replace the implementation of existing
methods with new implementations provided by the category.
Replace. Not override -- the original implementation is, for all
intents and purposes, lost.
You can use posing to effectively 'extend' a class such that
overridden methods can access super's implementation. But you really
don't want to do that for a number of reasons (not the least of which
is that subclasses may likely really want the various methods to
behave the way they do, encapsulation sometimes be damned).
Someone suggested swizzling. Just don't do it. Not ever in a piece
of production code unless you really *really* no what you are doing
(and, if you know what you are doing, it is unlikely that swizzling
would be the right answer!).
Getting back to the original (original) question, why do you need to
override a designated initializer anyway? Why can't you subclass?
Or change state after instantiation?
Generally, the need to change-- not augment, but actually change-- an
existing class's behavior through categories indicates a more
fundamental problem in body of code's architecture. I'm not saying
that it isn't a valid pattern -- just that it is extremely rare.
b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden