美文网首页
去除服务器返回值里面的长度为零的空字符串

去除服务器返回值里面的长度为零的空字符串

作者: 李某lkb | 来源:发表于2018-01-29 09:48 被阅读20次
    
    
    //调用方法
       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;
            
        }
        
     
        
    }
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:去除服务器返回值里面的长度为零的空字符串

          本文链接:https://www.haomeiwen.com/subject/uuotzxtx.html