Created
July 2, 2012 03:32
-
-
Save ttsubono/3030839 to your computer and use it in GitHub Desktop.
NSStringがnil, 空文字でYESを返す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NSStringについてはnullとnilと空文字""を判定しないといけない。 | |
// [NSNull null]はシングルトンなのでisKindOfClass:[NSNull class]を使わずとも==[NSNull null]で判定可能。 | |
// nilと空文字については、length==0でどちらも判別できる。 | |
// | |
// (self == (id)[NSNull null] || self.length == 0) | |
// | |
// カテゴリで定義して | |
// NSString+MyCategory.h | |
@interface NSString (MyCategory) | |
- (BOOL)isEmpty; | |
@end | |
// NSString+MyCategory.m | |
@implementation NSString (MyCategory) | |
- (BOOL)isEmpty { | |
return (self == (id)[NSNull null] || self.length == 0); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/968792/what-is-the-right-way-to-check-for-a-null-string-in-objective-c