好用的一些封装(持续更新)

1.判断字符串是否为空

+ (NSString *)isNullToString:(id)string{ string = [NSString stringWithFormat:@"%@",string]; if ([string isEqual:@"NULL"] || [string isKindOfClass:[NSNull class]] || [string isEqual:[NSNull null]] || [string isEqual:NULL] || [[string class] isSubclassOfClass:[NSNull class]] || string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@""] || [string isEqualToString:@"(null)"]){

        return @"";

    }else{

        return (NSString *)string;

    }

}

2.字典中所有字段自动判空

-(NSDictionary *)checkSrtingWithDictionary:(NSDictionary *)dict{

    NSMutableDictionary * tmpDic = [dict mutableCopy];

    NSArray * keys = [dict allKeys];

    [keys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        id value = tmpDic[obj];

        if ([value isKindOfClass:[NSDictionary class]]) {

            [tmpDic setObject:[self checkSrtingWithDictionary:value] forKey:obj];

        }else if ([value isKindOfClass:[NSArray class]]){

            [tmpDic setObject:[self checkSrtingWithArray:value] forKey:obj];

        }else{

            [tmpDic setObject:[NSString isNullToString:value] forKey:obj];

        }

    }];


    return tmpDic.copy;

}

3.数组中所有字段自动判空

-(NSArray *)checkSrtingWithArray:(NSArray *)array{

    NSMutableArray * tmpArr = [array mutableCopy];

    [tmpArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        id value = tmpArr[idx];

        if ([value isKindOfClass:[NSDictionary class]]) {

            [tmpArr replaceObjectAtIndex:idx withObject:[self checkSrtingWithDictionary:value]];

        }else if ([value isKindOfClass:[NSArray class]]){

            [tmpArr replaceObjectAtIndex:idx withObject:[self checkSrtingWithArray:value]];

        }else{

            [tmpArr replaceObjectAtIndex:idx withObject:[NSString isNullToString:value]];

        }

    }];


    return tmpArr.copy;

}

你可能感兴趣的:(好用的一些封装(持续更新))