Just a note, the NSAssert() Foundation function should only be called from inside an Objective-C method... if your code is somewhere in an Object-C class, this is fine, but if you're calling from inside a C- callback function or another C helper function (since you're creating a Library, this may be the case), and you want to use the Foundation method instead of the libc method (that I described earlier), make sure you use NSCAssert(myNum != nil, @"blah blah blah");

Also, to disable assertions in these cases, compile with - DNS_BLOCK_ASSERTIONS

/jason

On Jun 12, 2008, at 02:51 , 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.

G.

On 12 Jun 2008, at 3:50 pm, Ashley Perrien wrote:

Noob question: I'm putting together a small code library and I'm trying to include some error checking in the methods; what I want to protect against is the following:

NSNumber *myNum;

// Lots of code where I've forgotten to actually do anything with myNum

results = [myClass someCalculationUsing: myNum];

myNum in this case is an object and does exist but it's not a valid, usable argument. So in the implementation I'd like to have something along the lines of:

if (myNum == nil)
        NSLog(@"some error message");

but can't figure out what to check for that would get me into the error message condition. Any suggestions?

Ashley Perrien
_______________________________________________

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/graham.cox%40bigpond.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

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/jason.coco %40gmail.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

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]

Reply via email to