Yeah, variables you declare in a method (not as instance variables) are not automatically initialized to 0/NULL/nil for you.

If you try to use an uninitialized variable, it might point to some left over object in memory, or it might map to nothing at all. It is totally random.


What is the problem with just doing NSNumber *myNum = nil; ?
You can reassign it to a usable NSNumber object later, and if you don't, checking for equality with nil (or just doing if(!myNum) ) will let you catch that error.

Or am I missing something in your question....


Adam Leonard

On Jun 11, 2008, at 10: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/adam%40caffeinatedcocoa.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