how to test for NULL (or nil, or null)
how to test for NULL (or nil, or null)
- Subject: how to test for NULL (or nil, or null)
- From: Denis Stanton <email@hidden>
- Date: Mon, 1 Mar 2004 10:38:48 +1300
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.