Re: argument checks
Re: argument checks
- Subject: Re: argument checks
- From: Chris Suter <email@hidden>
- Date: Thu, 12 Jun 2008 17:03:42 +1000
On 12/06/2008, at 4:51 PM, Graham Cox wrote:
NSNumber* myNum = nil;
/* stuff */
NSAssert( myNum != nil, @"some error message");
[myClass calc:myNum];
Messages to nil are safe - it will treat your number as having a
value of 0. Thus as long as you initialise it to nil, your code will
run without crashing though of course may give incorrect results. If
you don't initialise it to anything, it will almost certainly crash.
The compiler should be warning you about this - if not, make it do so.
In the original example, myNum was being passed as a argument rather
than having a message to sent to it and it’s often not safe to pass
nil objects as arguments.
Another semi-related point is that the return value for messages to
nil is something to watch out for. For example, -[NSNumber intValue]
will reliably return 0 for nil objects whereas -[NSNumber
longlongValue] won’t (on some architectures at least).
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden