//调用方法
if ([responseObject isKindOfClass:[NSDictionary class]]) {
responseObject = [[NetWorkManager sharedManager] removeEmptyStrAtDictionary:responseObject];
}
//字典里去除空
- (id)removeEmptyStrAtDictionary:(id)dict {
//空字符串直接不要
if ([dict isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary* tmp = [(NSDictionary*)dict mutableCopy];
__weak typeof(self) weakSelf = self;
[tmp enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
__strong __typeof(self) strongSelf = weakSelf;
if ([obj isKindOfClass:[NSString class]] && ((NSString*)obj).length <= 0 ) {
[tmp removeObjectForKey:key];
}else if ([obj isKindOfClass:[NSDictionary class]]){
[tmp setObject:[strongSelf removeEmptyStrAtDictionary:obj] forKey:key];
}else if ([obj isKindOfClass:[NSArray class]]){
[tmp setObject:[strongSelf removeEmptyStrAtArray:obj] forKey:key];
}
}];
dict = [tmp mutableCopy];
}
return dict;
}
- (NSArray*)removeEmptyStrAtArray:(id)orginArray {
if ([orginArray isKindOfClass:[NSArray class]]){
NSMutableArray*array = [orginArray mutableCopy];
NSMutableArray*endArray = [NSMutableArray array];
__weak typeof(self) weakSelf = self;
[array enumerateObjectsUsingBlock:^(id _Nonnull tmp, NSUInteger idx, BOOL * _Nonnull stop) {
__strong __typeof(self) strongSelf = weakSelf;
if ([tmp isKindOfClass:[NSDictionary class]]) {
[endArray addObject: [strongSelf removeEmptyStrAtDictionary:tmp]];
}else if ([tmp isKindOfClass:[NSArray class]]){
[endArray addObject:[strongSelf removeEmptyStrAtArray:tmp]];
}else if ([tmp isKindOfClass:[NSString class]] && ((NSString*)tmp).length <= 0 ) {
}else {
[endArray addObject:tmp];
}
}];
return endArray;
}else {
return orginArray;
}
}
网友评论