Re: Question about floatValue on nil object
Re: Question about floatValue on nil object
- Subject: Re: Question about floatValue on nil object
- From: Jack Nutting <email@hidden>
- Date: Tue, 13 Sep 2005 10:58:35 +0200
On 9/13/05, Michaël Parrot <email@hidden> wrote:
>
> Hi,
>
> I am small question. I get a NSNumer from a NSDictionary (or other,
> not important) and this object is equal to nil.
>
> NSNumber *n=nil;
>
> NSLog(@"la %f",[n floatValue]);
> NSLog(@"la %d",[n intValue]);
>
> My problem is that the second nslog show 0 value but floatValue
> return aleatory value.
>
> What is the problem ?
>
> Sure, I can check if number if equal to nil, but I am suprised that
> [nil floatValue] don't return nil.
>
While sending a message to nil is generally OK, there is one caveat: Methods
returning any non-object values (int, float, etc) will return a value that
is essentially undefined. Often, nil-sent messages returning a float or an
int will seem to return 0, but in fact that is more or less a lucky accident
based on what is in the processor registers, and even if it works today, it
may not work on another architecture in the future. (I saw this explained in
detail somewhere a while ago, but unfortunately can't find it...)
In short, if you're going to send a message that returns a basic type
instead of an object, you should *always* first make sure the receiver isn't
nil.
--
// jack
// http://www.nuthole.com
_______________________________________________
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