Skip to content

Instantly share code, notes, and snippets.

@YusukeHosonuma
Created April 20, 2014 09:50
Show Gist options
  • Save YusukeHosonuma/11110027 to your computer and use it in GitHub Desktop.
Save YusukeHosonuma/11110027 to your computer and use it in GitHub Desktop.
Objective-Cでカテゴリ拡張およびUtilクラスによる空文字列チェック ref: http://qiita.com/YusukeHosonuma/items/999d47a269435d094210
if ([someString length])
#import "NSString+Additions.h"
@implementation NSString (Additions)
- (BOOL)isNotEmpty {
return [self length] > 0;
}
@end
#import "StringUtils.h"
@implementation StringUtils
+ (BOOL)isEmpty:(NSString*)string {
return string == nil || [string length] == 0;
}
+ (BOOL)isNotEmpty:(NSString*)string {
return ![StringUtils isEmpty:string];
}
@end
- (void)test_StringUtils_isEmpty
{
XCTAssertFalse([StringUtils isEmpty:@"foo"]);
XCTAssertTrue ([StringUtils isEmpty:@""]);
XCTAssertTrue ([StringUtils isEmpty:nil]);
}
- (void)test_StringUtils_isNotEmpty
{
XCTAssertTrue ([StringUtils isNotEmpty:@"foo"]);
XCTAssertFalse([StringUtils isNotEmpty:@""]);
XCTAssertFalse([StringUtils isNotEmpty:nil]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment