• 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: Warning about comparison between pointer and integer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Warning about comparison between pointer and integer


  • Subject: Re: Warning about comparison between pointer and integer
  • From: Fritz Anderson <email@hidden>
  • Date: Wed, 16 Dec 2009 10:41:33 -0600

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 (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

References: 
 >Warning about comparison between pointer and integer (From: "Eric E. Dolecki" <email@hidden>)

  • Prev by Date: Re: Warning about comparison between pointer and integer
  • Next by Date: Re: Warning about comparison between pointer and integer
  • Previous by thread: Re: Warning about comparison between pointer and integer
  • Next by thread: Re: Warning about comparison between pointer and integer
  • Index(es):
    • Date
    • Thread