[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Subclassing NSString?
From: |
Richard Frith-Macdonald |
Subject: |
Re: Subclassing NSString? |
Date: |
Wed, 3 Jan 2001 10:20:17 +0000 |
On Tuesday, January 2, 2001, at 12:09 AM, Frederic wrote:
> Hi,
>
> I need to create NSString which are unique in memory. (I actually need
> that for performance reasons).
> So I decided to subclass NSString and create a class method
> +(id)keyWithString:(NSString*)keyName which
> looks first in a hashtable to ensure it returns a singleton.
> But when invoking [[MyNSStringSubClass] alloc] initWithString:keyName],
> it fails saying:
> "subclass ASKey should override
> initWithCharactersNoCopy:length:freeWhenDone:"
>
> Is this really necessary and how should I do that?
Probably you want to add a category of NSString that implements the
+keyWithString: method, rather than subclassing.
If there is some reason that you actually need a subclass ... then,
because of the nature of class clusters, you *do* need to do more work
for your subclass, since the public NSString class is abstract ... it
doesn't actually store any string data, but depends on a few 'primitive'
methods to access the data storage provided by its concrete subclasses.
For NSString, the primitives (in GNUstep) are -
-initWithCharactersNoCopy:length:freeWhenDone:
-characterAtIndex:
-length
while NSMutableString also requires -
-initWithCapacity:
-replaceCharactersinRange:withString:
In both cases, you will need to implement -dealloc to release the
storage your subclass uses.
In MacOS-X, it's recommended that you override *all* the initialisers for
the class cluster. I've tried to ensure that the GNUstep implementation
of the initialisers will do something sensible as long as the two I
mentioned above are implemented in your subclass.