I have a method where I want to make sure I do something special when I encounter an "empty" string. It works great when it actually has a string, or when I specifically set it to "nil" or "NULL", but it crashes when I try to test an uninitialized NSString. How do I handle that scenario?
For example:
-(void) WhyDoICrash
{
NSString *foo;
// Some other stuff...
// foo should be considered "empty"
if (foo == nil || foo == NULL || [foo length] == 0)
{
// whatever
}
}
============= EDIT ===================
Ok, the above was just a REALLY simple example that displays the problem. I'm actually using a category for NSString, which means some of the (good, but not applicable) suggestion I'm getting can't be used. Here is my exact code:
#pragma mark - NSString Category
@interface NSString (NullOrEmptyTesting)
+(BOOL) isNullOrEmpty: (NSString*)input;
@end
@implementation NSString (NullOrEmptyTesting)
+(BOOL) isNullOrEmpty: (NSString*)input
{
return (input == (id)[NSNull null] || input.length == 0);
}
@end