經(jīng)常性我們會用到字符串判空,如下
#import "NSString+Tools.h"
@implementation NSString (Tools)
+ (BOOL)isBlankString:(NSString *)aStr {
if (!aStr) return YES;
if ([aStr isKindOfClass:[NSNull class]]) return YES;
if (![aStr isKindOfClass:[NSString class]]) return YES;
if (!aStr.length) return YES;
return NO;
}
@end
但是有時候我們從服務(wù)器獲取數(shù)據(jù)的時候不一定為string所以做了一點優(yōu)化
+ (BOOL)isBlankString:(NSString *)aStr {
if (!aStr) return YES;
if ([aStr isKindOfClass:[NSNull class]]) return YES;
if (![aStr isKindOfClass:[NSString class]]){
NSString *tempStr = [NSString stringWithFormat:@"%@",aStr];
aStr = tempStr;
}
if (!aStr.length) return YES;
return NO;
}
+ (NSString *)getString:(NSString *)aStr {
if ([self isBlankString:aStr]) {
return @"";
}
return [NSString stringWithFormat:@"%@",aStr];
}