SO
I wish to check to see if the item in my array [clientDataArray objectForKey:@"ClientCompany"] is nil.
temp = [clientDataArray objectForKey:@"ClientCompany"];
if (temp != [NSNull null]) infofieldCompany.text = temp;
So far I have been able to achieve this through the above code, but it does give me the warnings
- warning:
NSArraymay not respond to-objectForKey: - warning: comparison of distinct
Objective-C types
struct NSNull *andstruct NSString *lacks a cast
My main interest is the second warning, but the first warning also interest me. How should I adapt my above code?
Your first warning looks like you're trying to call objectForKey on an NSArray. Which isn't going to work, as NSArray doesn't have an objectForKey method.
As for the second warning you can just compare directly with nil, ie:
if (temp != nil)
or since nil is equivalent to 0, you can also just do:
if (temp)
After trying all the options, i think this is the best option to comapre NSString null
if ( temp != ( NSString *) [ NSNull null ] )
{
// do some thing
}
Both of the answers previously given missed a fundamental point: you can't put nil into an array, so you'll never get nil out of an array. Using NSNull as a placeholder in an array is the correct thing to do, however your variable temp then cannot be declared as an NSString *, as it might not be one. Use either NSObject * or id as the type of the variable to suppress the comparison warning.
The best solution to test NULL or Empty values:
if (yourObj.yourString == (NSString*) [NSNull null] || yourObj.yourString.length == 0 )
{
yourObj.yourString = @"";
}
Quiet simple
if (myString == nil){
NSLog(@"myString is null");
}
I think the problem is (I mean the second warning) is that you're comparing NSString object, which could be set to null to an NSNull object.
Have you tried the usual C way of checking for null?
if(temp) {
// It's not null, do something.
}
I'm not 100% sure about this one, but you could try it. If you did, sorry that couldn't provide more useful information.
Good luck!
The NSNull singleton can be used to build "interleaved" arrays like obj0, obj1, obj2, NSNull, obj3, NSNull, ..., nil.
warning: 'NSArray' may not respond to'-objectForKey:'
NSArray does not implement objectForKey.
Your code will crash at runtime (if clientDataArray has been allocated and initialized)
You can access array elements by index (objectAtIndex:).
If you need to associate objects with keys, take a look at NSDictionary.
if (temp != nil)
or since nil is equivalent to 0, you can also just do:
if (temp)
I am not by any stretch of the imagination a Obj-C or Cocoa expert, in C/C++ you can use something like:
if(somePtr != NULL)
And, unless you explicitly, set it to NULL or changed what it was pointing to, you can be certain, that it is, in fact, Not Null or Null (what-ever you were looking for...)
But I have noticed (my personal experience anyway) that in Obj-C,
If you do something like: if(someObj != nil) or the converse,
There's no Guarantee, it will tell you it's REAL status...
So (after dealing with a bunch of crashes) when I deal with Obj-C (BS), to be safe, I always have a BOOL or flag set up to track it's status, so you don't end up Free-ing, excuse me... "Release-ing" something that has already be deallocated...
Even though this question is old, I hope My Answer will help some one else.
You can try this
temp = [clientDataArray objectForKey:@"ClientCompany"];
if (![temp isKindOfClass:[NSNull class]]) infofieldCompany.text = temp;
来源:https://stackoverflow.com/questions/2401234/objective-c-how-to-check-if-a-string-is-null