On 16 Dec 2009, at 10:18 AM, Eric E. Dolecki wrote:

> I am using NSUserDefaults to store some data. On first run of the
> application, I check for the data, and if it's not been set already, I set
> it.

This is done, and better, by -[NSUserDefaults registerDefaults:].

> I however get warnings in my code. In my .h I set up my variables, and then
> in my .m:
> 
> NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
> alarm1_hour = [prefs integerForKey:@"alarm1Hour"];
> if( alarm1_hour == NULL ){
>  NSLog(@"alarm 1 hour is null" );
> }

You don't say, but I assume (hope) that alarm1_hour is declared as an integer. 
NULL is (at least notionally) declared as (void *)0; it's a pointer. Comparing 
them is almost always a mistake. Code written after 1988 or so should not treat 
integers and pointers as interchangeable.

Besides, -[NSUserDefaults integerForKey:] is documented (you read the 
documentation?) to return 0 if the key doesn't appear in the user defaults. 
Scalars (as in not-pointers) don't have "I'm not set" values (like NULL for 
pointers); every possible value may be meaningful. You may have "sentinel" 
values (valid, but you know to treat them as signals of a condition, rather 
than in-band values), but that's another story.

> Should I use a temporary variable to check instead of the variable with the
> pointer? Or are these warnings okay to ignore?

You don't ignore warnings, except on the most serious consideration, such as 
when the warning is about a condition that your code independently verifies 
cannot happen.

        — F

_______________________________________________

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 arch...@mail-archive.com

Reply via email to