经常性我们会用到字符串判空,如下
#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
但是有时候我们从服务器获取数据的时候不一定为string所以做了一点优化
+ (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];
}