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
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.
1.4m articles
1.4m replys
5 comments
57.0k users