Re: Abstract objects (Newbie)
Re: Abstract objects (Newbie)
- Subject: Re: Abstract objects (Newbie)
- From: Ali Ozer <email@hidden>
- Date: Tue, 5 Feb 2002 11:28:01 -0800
On Tuesday, February 5, 2002, at 09:36 AM, Steve Bird wrote:
>
I'm confused about something pretty basic here. I have plenty of
>
object oriented experience, but perhaps that's a hindrance here.
>
I have the following files, attempting to create a descendant of
>
NSString:
As others have pointed out, NSString is a class cluster, and there are
special considerations for subclassing. You should look at the
documentation for details, but as this question comes up often, here are
the basics on this.
Class clusters are a group of classes which are represented by an
abstract public class, with usually hidden implementation classes
underneath. Because the public class is abstract, you usually need to
provide a concrete implementation when subclassing. However, it is also
usually the case that subclassing class clusters is not encouraged.
A general philosophy in low-level value classes such as NSString is that
their values are clearly defined: All strings are composed of a number
of Unicode; so the basic accessors of an NSString are the number of
characters and the characters themselves. Subclassing for purposes of
changing this is considered bad, because now you get into issues such as
[string1 isEqual:string2] but not [string2 isEqual:string1].
If you want to subclass in order to add additional functionality (say,
for instance, you want to add a method which returns the reversed
version of the string; or find with regular expression), best way to do
is via a category, which adds the functionality to all NSString classes.
One good reason to subclass strings is to provide alternate
implementations. For instance, you might have string whose contents can
be computed easily, or is backing store is a giant file on the network;
in those cases, rather than having an array of characters in memory, it
might be best to get or compute the contents lazily. In those cases, in
order to subclass, just implement the methods length, characterAtIndex:,
and getCharacters:range: (the latter for performance); and you're done.
Everything else in NSString, including all categories anyone has added
to NSString, just work. If you want, you can also add additional methods
which work on that particular subclass for performance reasons; then the
users will need to know they have an instance of your subclass.
(NSMutableString is a famous example of this.)
Note that if you subclass NSString, you don't get the existing init
methods, because it's assumed you are subclassing in order to provide
your own custom implementation, which presumably requires your own init
method. So, you don't inherit methods such as initWithString;.
Other examples of class clusters or classes with true abstract
implementations in Cocoa include the collections (NSArray, etc); NSData,
NSColor, NSAttributedString.
Ali
_______________________________________________
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.