Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C how to check if a string is null

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: NSArray may not respond to -objectForKey:
  • warning: comparison of distinct Objective-C types struct NSNull * and struct 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?

like image 769
oberbaum Avatar asked Mar 08 '10 12:03

oberbaum


People also ask

How check string is null or not in Objective C?

if (title == (id)[NSNull null] || title.

What is NSString?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.


2 Answers

After trying all the options, i think this is the best option to comapre NSString null

if ( temp != ( NSString *) [ NSNull null ] )
{
  // do some thing
}
like image 173
Robinson Avatar answered Oct 07 '22 19:10

Robinson


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)
like image 42
Tom Jefferys Avatar answered Oct 07 '22 18:10

Tom Jefferys