次のコードがあります。
NSString *content = [[NSUserDefaults standardUserDefaults] stringForKey:@"mykey"];
NSLog(@"string is %@",content);
if ([content stringIsEmpty]){
NSLog(@"empty string");
}else{
NSLog(@"string is not empty");
}
stringIsEmpty は のクラス カテゴリNSString
です:
- (BOOL ) stringIsEmpty {
if ((NSNull *) self == [NSNull null]) {
return YES;
}
if (self == nil) {
return YES;
} else if ([self length] == 0) {
return YES;
}
return NO;
}
出力は次のとおりです。
string is (null)
string is not empty
同時に空ではなく、null である可能性はありますか?