Re: Number Formatting
Re: Number Formatting
- Subject: Re: Number Formatting
- From: Jon Hull <email@hidden>
- Date: Sun, 20 Jun 2004 19:19:32 -0700
On Jun 20, 2004, at 5:02 PM, Daniel Todd Currie wrote:
How are NSNumbers, NSDecimalNumbers, etc. even stored though? How is
the formatting of these numbers retained? If I'm going to subclass
NSDecimalNumber, should I be mucking around in all these @private
instance variables? Furthermore, where oh where is the NSNumber
header file? (It's not in Foundation.framework as expected.)
I guess a few tips on strategy here might be appreciated, since I also
have recently decided that I'm going to open my work on a whole slew
of math/number classes in an open-source framework. If any of you
have any interest in this project not sucking, please be compelled to
chime in. ;)
The header is in NSValue.h
As for implementation, you have two options. In either case, I
wouldn't mess with the @private stuff. Basically, all you need to do
is store the extra information, and let NSNumber (or NSDecimalNumber)
do its thing.
The more efficient of your two options is to directly subclass.
Basically you take the default initializer (I think it is: -
(id)initWithBytes:(const void *)value objCType:(const char *)type ) and
stick your stuff onto it like this (written in Mail):
- (id)initWithBytes:(const void *)value objCType:(const char *)type
significantFigures:(unsigned)sigFigs
{
if(self=[super initWithBytes:value objCType:type]){
if(sigFigs==NSNotFound){
//take best guess at sig-figs here
}else{
sigFigsIVar=sigFigs;
}
}
return self;
}
Make sure you also override the default initializer like so:
- (id)initWithBytes:(const void *)value objCType:(const char *)type
{
return [self initWithBytes:value objCType:type
significantFigures:NSNotFound];
}
You may also want to override initWithString: and stringValue and
create a convenience initializer or two. For instance:
-(id)initWithDouble:(double)value
significantFigures:(unsigned)sigFigs;
OPTION 2: If you find that this becomes too complicated, or discover
that NSNumber doesn't respect the whole default initializer thing (I
can't remember offhand whether NSNumber was one of those), then you can
go the simpler route of creating a class which contains an NSNumber
instance and any extra iVars you need. Then you simply pass any of the
number related stuff that you need off to your NSNumber like so:
-(int)intValue
{
return [myNumberIVar intValue];
}
If you go this route, then I would recommend reading up on message
forwarding.
Personally, I prefer the second option... but it is really a matter of
taste.
Thanks,
Jon
_______________________________________________
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.