How to Check for a Null String in Objective-C

What is the right way to check for a null string in Objective-C?

As others have pointed out, there are many kinds of "null" under Cocoa/Objective C. But one further thing to note is that [title isKindOfClass:[NSNull class]] is pointlessly complex since [NSNull null] is documented to be a singleton so you can just check for pointer equality. See Topics for Cocoa: Using Null.

So a good test might be:

if (title == (id)[NSNull null] || title.length == 0 ) title = @"Something";

Note how you can use the fact that even if title is nil, title.length will return 0/nil/false, ie 0 in this case, so you do not have to special case it. This is something that people who are new to Objective C have trouble getting used to, especially coming form other languages where messages/method calls to nil crash.

How do I test if a string is empty in Objective-C?

You can check if [string length] == 0. This will check if it's a valid but empty string (@"") as well as if it's nil, since calling length on nil will also return 0.

How to check the NULL value in NSString in iOS?

In Objective-C and Cocoa, the property may not be set—that is, it's nil—or it may be set to the object representation of nil, which is an instance of NSNull. You probably want to check for either of these conditions, like this:

NSString* categoryName = appDelegate.categoryName;
if (categoryName == nil || categoryName == (id)[NSNull null]) {
// nil branch
} else {
// category name is set
}

This will execute the nil branch if the categoryName property is set to nil (the default for all properties), or if it's been explicitly set to the NSNull singleton.

Objective-C how to check if a string is null

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)

Checking a null value in Objective-C that has been returned from a JSON string

is how the NSNull singleton logs. So:

if (tel == (id)[NSNull null]) {
// tel is null
}

(The singleton exists because you can't add nil to collection classes.)

how to Check NSString is null or not

Like that:

[myString isEqual: [NSNull null]];

NSString nil or empty check -- is this complete?

I only use the next conditional and do not even need a category:

if (!aString.length)
{
...
}

Using Objective-C theory, a message to NIL will return nil or zero, so basically you do not have to test for nil.

How to check if an object is nil in Objective-C?

You can do it normally just by checking the object itself. There is also a good explanation on NSHipster for NSNull.

if( myObject ){
// do something if object isn't nil
} else {
// initialize object and do something
}

otherwise just use

if( myObject == nil ){

}

null, nill is string type in Objective-C

There are several values that are different from Objective-C perspective here:

  1. nil (aka null in other languages)
  2. [NSNull null] (a special marker value object)
  3. @"" (empty string)
  4. @"null" (just a string with 4 characters)

If you write your dataDic from your app, and you know that your app handles it well, you don't have to check all the cases. Check only the ones you expect. For example, if your app only writes non-empty strings to the dictionary, but sometimes "email" is not there, you only have to check nil, because objectForKey returns nil if the value is not inside the dictionary.

On the other hand if you have obtained dataDic from a 3rd party API, decoded from JSON for example, then you should do the full checking:

  1. [NSNull null] is placed inside the dictionary if JSON has null originally like {"email":null}
  2. If the server API changed you might get some other structure than NSString there (although quite unlikely here).
  3. nil is returned if you don't have the key/value at all.

You can rule out all the 3 checks at once by doing:

NSString *emailStr = nil;
id emailObj = [dataDic objectForKey:@"email"];
if ([emailObj isKindOfClass:[NSString class]]) {
emailStr = emailObj;
}

Note that you might not have to check for an empty string or nil before assigning to UILabel text, because those work fine and just erase the label text:

self.emailLbl.text = @"";
self.emailLbl.text = nil; // another way to erase


Related Topics



Leave a reply



Submit