• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: argument checks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: argument checks
      • From: Graham Cox <email@hidden>
References: 
 >argument checks (From: Ashley Perrien <email@hidden>)
 >Re: argument checks (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: argument checks
  • Next by Date: toggleToolbarShown: How do I localize the menu item strings for non-supported languages?
  • Previous by thread: Re: argument checks
  • Next by thread: Re: argument checks
  • Index(es):
    • Date
    • Thread