How to Test If a String Is Empty in Objective-C

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.

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.

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.

Objective C: A cleaner way to check if a string isn't empty before creating and assigning a string to a dictionary key?

Something like this

- (void)updateDic:(NSMutableDictionary *)dic withString:(NSString *)str {
if (!str || [str isEqualToString:@""]) {
return;
}

dic[str] = str;
}

And then just iterate over all strings and use that method.

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)

How to determine if NSString is empty

if ([string length] == 0) {
// do something
}

If the string is nil, then the message to nil will return zero, and all will still be well.

Check for an empty string

To compare an NSString to another one, you would use isEqualToString. You're checking for inequality, so it would be:

NSString *text = ...;

if (![text isEqualToString:@""]) {
...
}

But really, since you're just checking if the string is empty, you'll want something like

NSString *text = ...;

if ([text length] != 0) {
...
}

Note that if text is nil, the code in the if-statement will not execute. This is because [nil length] will return 0. For more information about that, see "Sending Messages to nil" in Apple's documentation.


I suspect mystring.text is an NSString in your case, so it would be

if ([mystring.text length] != 0) {
myPath = [myPath stringByAppendingString:mystring.text];
}

How to check if UITextFields are empty?

I am supposing UITextField variable as *tfield so here is the solution

if (tfield.text.length > 0 || tfield.text != nil || ![tfield.text isEqual:@""])
{
//do your work
}
else
{
//through error
}


Related Topics



Leave a reply



Submit