不知道这样写会不会有问题 遇到的大神帮忙看看
字典赋值 如果赋的空值就会崩溃
1.字典取值是不会崩溃的 这样都不崩溃
NSDictionary *dicc = NULL;
NSString *str = dicc[@"2123"];
- 预防取值崩溃
- (NSArray *)getSafeArray:(id)array
{
if ([array isKindOfClass:[NSArray class]]) {
return (NSArray *)array;
} else {
return [NSArray array];
}
}
- (NSString *)getSafeString:(id)string
{
if ([string isKindOfClass:[NSString class]])
return (NSString *)string;
else
return @"";
}
- (NSDictionary *)getSafeDictory:(id)dic
{
if ([dic isKindOfClass:[NSDictionary class]])
return (NSDictionary *)dic;
else
return [NSDictionary dictionary];
}
3.判断是否是一个空的字符串 比如@"" @" "
- (BOOL) isBlankString:(NSString *)string
{
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
网友评论