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: Denis Stanton <email@hidden>
- Date: Mon, 1 Mar 2004 14:24:22 +1300
Thank you Louis
The first part of this I had worked out, but your suggestion to use
"respondsToSelector:@selector(intValue)" makes a lot of sense. It's
more elegant, because the only reason for this test is to bypass code
that sometimes fails on the intValue selector.
Thanks for the help, and for extending my education with both
respondsToSelector and an explanation of the way [object description]
has created the NULL result for %@.
Denis
On Monday, March 1, 2004, at 12:13 PM, Louis C. Sacha wrote:
You could also use the isEqual: method for the test
if ([myNumber isEqual:[NSNull null]])
I would actually suggest using a different sort of test, which would
check the class of the returned object instead:
if ([myNumber isKindOfClass:[NSNumber class]]) { ... object is an
NSNumber ... }
else { ... object could be NSNull or some other class ... }
This has the benefit of catching anything that is not the class you
expect, for example if somehow the object was actually an NSString
instead of an NSNumber for some reason.
Although, come to think of it, NSString responds to intValue also and
might be able to provide a valid result anyway, so an even better test
might be
if ([myNumber respondsToSelector:@selector(intValue)]) { ... you can
ask for object's intValue ... }
else { ... object could be NSNull or some other class that doesn't
respond to intValue... }
It may not be possible in your case for the returned object to be
anything other than an NSNumber or [NSNull null], but if the
dictionary is coming from a source you don't control, it might be a
good idea to use either of the more comprehensive tests.
Hope that helps,
Louis
_______________________________________________
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.