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
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]