Re: Subclassing appendFormat: for NSMutableString?
Re: Subclassing appendFormat: for NSMutableString?
- Subject: Re: Subclassing appendFormat: for NSMutableString?
- From: Ondra Cada <email@hidden>
- Date: Sun, 16 May 2004 17:59:51 +0200
Nikolaus,
On 16.5.2004, at 16:22, Dr. H. Nikolaus Schaller wrote:
>
What I want is a class that behaves in some aspects like a
>
NSMutableString but has additional instance variables. A typical
>
candidate for subclassing. Documentation recommends: Write a subclass
>
of NSObject that manages an NSMutableString as an instance variable
>
and write wrapper methods.
>
>
Well, but how do I wrap appendFormat: which is the most important
>
method I intend to provide by the subclass?
Uh? You just implement it in the new class. What exactly do you mean by
"wrap"?
>
a) no Subclass of NSMutableString - because it is a class cluster
More or less.
>
b) no Wrapper around NSMutableString - because you can't wrap methods
>
with variable argument lists
Uh-oh?
@interface MyCountedMutableString:NSMutableString {
NSMutableString *contents;
int n;
}
-(void)appendFormat:(NSString*)fmt,...;
@end
@implementation MyCountedMutableString
-(void)appendFormat:(NSString*)fmt,... {
va_list al;
va_start(al,fmt);
NSString *adding=[[[NSString alloc] initWithFormat:fmt arguments:al]
autorelease];
va_end(al);
[contents appendFormat:@"%@ (%d)",adding,n++];
}
... standard primitive overrides, forwarding if needed, other wrappers
...
@end
Well it's written directly in Mail, so typos, overlookings etc.
possible, but in principle this should and would work.
>
c) no Category of NSMutableString - because you can't add instance
>
variables to a Category
You can, of course (you can do pretty *anything*, which is the charm of
ObjC ;)), for a price of somewhat ugly API and a bit inefficient
access: you use a static dictionary indexed by the instance id
(nonretained). IIRC, it was discussed here not too long ago in details.
>
The only idea I have is use b) and leave out the appendFormat: method
>
and replace its calls by [myobject appendString:[NSString
>
stringWithFormat:...]]. It a little ugly and wasting space&time.
Perhaps I just don't follow properly: what's exactly the problem?!?
---
Ondra Hada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.