Matt H.
Matt H.

Reputation: 10796

NSString "nil or empty" check -- is this complete?

I was writing a small Category on NSString, and I wanted to know if this method is accurately handles all potential use cases:

Update: to clarify -- I wanted to make sure I'm not missing some oddball case involving character encodings, etc..

@implementation NSString (Helpers)

+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
    if (!aString)
        return YES;
    return [aString isEqualToString:@""];
}
@end

Sample usage:

-(void) sampleUsage {
    NSString *emptyString = @"";
    NSString *nilString = nil;
    NSAssert([NSString stringIsNilOrEmpty:nilString] == YES, @"String is nil/empty");
    NSAssert([NSString stringIsNilOrEmpty:emptyString] == YES, @"String is nil/empty");
}
@end

Upvotes: 39

Views: 55528

Answers (6)

Rahul
Rahul

Reputation: 10635

Make sure to check for spaces, trim white spaces before calculating length.

+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
    return !aString || [[aString  stringByTrimmingCharactersInSet:
                    [NSCharacterSet whitespaceCharacterSet]] length] == 0;
}

Upvotes: 0

Ash
Ash

Reputation: 5712

Simply Check your string length

> if (!yourString.length){
>     //your code  } a

message to NIL will return nil or 0, so no need to test for nil :).

Happy coding ...

Upvotes: 0

fatuhoku
fatuhoku

Reputation: 4911

@dasblinkenlight's answer is fine, but a much more readable conditional check I would use is:

NSString *string = ...;   // define the string
if ([string length] == 0) {
    // Do stuff with the string
} else {
    // The string is empty or nil here
}

Very concise and does not require a separate convenience function definition. It's easy enough to remember.

EDIT: @Michael G. Emmons posted this as the last comment to that answer... credit to him but I'm listing this as an answer in its own right.

Upvotes: 10

orj
orj

Reputation: 13484

Some examples of this sort of "is not empty or blank" tests as a category on NSString.

// Please note that in general I advocate using a prefix on category methods
// to avoid category collisions.  I've not done this here for clarity. 
// The @interface is also excluded from this example for brevity.

@implementation NSString (MyAdditions)

- (BOOL)isNotEmpty
{
  return [self length] != 0;
}

- (BOOL)isNotBlank
{
  if ([self isNotEmpty])
  {
    NSCharacterSet *nonWhitespaceSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
    NSRange range = [self rangeOfCharactersFromSet:nonWhitespaceSet];
    return range.location != NSNotFound;
  }

  return NO;
}

@end

Upvotes: 2

Legoless
Legoless

Reputation: 11112

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.

Upvotes: 138

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You can simplify the code by removing conditional:

+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
    return !(aString && aString.length);
}

Upvotes: 30

Related Questions