Re: NSNumber is completely broken
Re: NSNumber is completely broken
- Subject: Re: NSNumber is completely broken
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Thu, 26 May 2005 09:45:14 +0200
--- Todd Blanchard <email@hidden> wrote:
I'm working on bridging Cocoa to another language and have found that
NSNumber is totally broken.
...
These are the objective C types recorded for various C types
...
Well, you are pointing out that NSNumber stores your data in a
non-obvious way. This means not that NSNumber is totally broken, but
that the documentation could be improved.
It is just so that NSNumber _only_ stores some bytes and is prepared to
return the same bytes to you, but _not_ the encoding of these bytes.
The inherited -objCType method just gives a hint on the data size - not
more.
So if you get something stored as 'i' you do not know whether is was a
rather large unsigned int, unsigned short, or unsigned char, or a small
negative signed int, signed short, or signed char.
uint8_t u8af = 0xaf ;
id uu = [ [ NSNumber alloc ] initWithUnsignedChar: u8af ];
uint32_t u32af = [ uu unsignedShortValue ];
// now u32af is 0xffaf
So there is the very easy rule (which really should be in the
documentation - I was fooled by NSNumber myself):
if you use NSNumber you must someplace else remember the _type_ of the
data stored in NSNumber.
Or you should use NSValue and "- (id)initWithBytes:(const void *)value
objCType:(const char *)type"
NSNumber is a subclass of NSValue but does not behave like one.
It has _less_ functionality than its superclass (e.g. objCType),
and it uses _less_ storage:
NSNumber with double uses 16 bytes, whereas NSValue uses 32.
NSNumber with BOOL uses 0 bytes, whereas NSValue uses 16.
So for storing 10 million BOOLs this makes quite a difference.
(All this tested on 10.4.1)
Also NSValue seems to be slightly slower.
Kind regards,
Gerriet.
_______________________________________________
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