Re: how to test for NULL (or nil, or null)
Re: how to test for NULL (or nil, or null)
- Subject: Re: how to test for NULL (or nil, or null)
- From: Pete Yandell <email@hidden>
- Date: Mon, 1 Mar 2004 09:16:50 +1100
The short answer is that nil and NULL are both equivalent to zero, so
your first two tests are asking:
if (myNumber == 0)
(This is, of course, testing the pointer to myNumber against zero
rather than testing the value of myNumber.)
[NSNull null] on the other hand is "a unique object used to represent
null values in collection objects (which dont allow nil values)",
hence the fact that it gets returned in place of nil by your
valueForKey: call.
If you want to avoid the compiler warning, do a bit of creative casting:
if ((id)myNumber == (id)[NSNull null])
should work. You might even be able to get away with just casting one
of them to id...have a play around.
Pete Yandell
http://pete.yandell.com/
On 01/03/2004, at 8:38 AM, Denis Stanton wrote:
I'm sure this is a question that has been asked before, but I can't
find it despite keeping the last 22,210 messages on this list in my
mailbox.
I have a cocoa / objective-C app that reads from a MySQL database that
unfortunately has some NULL values where there should be numbers. As
these NULLs tend to respond badly to a request for intValue I need to
detect them before applying that code.
To my surprise I find that tests for == nil do not work. After much
experimentation, and a little research (might have been better the
other way) I now have code that works, but the compiler gives me a
warning message, which makes me think the code could be better.
can anybody help me unravel the various forms of nil/null/NULL ?
Here's what I have (simplified):
NSNumber *myNumber = [myDictionary valueForKey: @"MyNumber"];
NSLog(@"myNumber = %@", myNumber); // output myNumber = NULL
if (myNumber == nil)
NSLog(@"test 1 myNumber == nil); // never happens
if (myNumber == NULL)
NSLog(@"test 2 myNumber == NULL); // never happens
if (myNumber == [NSNull null])
NSLog(@"test 3 myNumber == [NSNull null]);
The third test works, so I get output:
myNumber = NULL
test 3 myNumber == [NSNull null]
Unfortunately the compiler doesn't think much of the test that works,
and warns "comparison of distinct pointer types lacks a cast"
I'm thinking that the valueForkey finds a match, but that match is a
NULL, so myNumber gets to have value NULL rather than having no value.
This is why "if (myNumber == nil)" doesn't work, but why doesn't "if
(myNumber == NULL)" work? My NSLog line claims that myNumber has a
value NULL, but a test for == NULL does not succeed.
Even though I now have code that works I'm not happy with that warning
message.
Denis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.