从Dictionary中查找空值的捷径技术?

时间:2016-02-09 10:07:14

标签: ios objective-c iphone nsdictionary

我在nsmutabledictornary中有100个键和值,我想检查任何值是否为null。你有短暂的功能或技术吗?

我不希望多行代码检查每个键和值。您的回答将不胜感激。

3 个答案:

答案 0 :(得分:2)

此代码将为您提供具有(非)空值的键集。您无法在字典中存储实际的nil值,因此假设为[NSNull null]。该谓词可以轻易地改变任何其他条件。

NSDictionary *d = @{ @"a" : @"1", @"b" : [NSNull null] };

NSSet *nullKeys = [d keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
    return [d[key] isKindOfClass:[NSNull class]];
}];

NSSet *nonnullKeys = [d keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
    return [d[key] isKindOfClass:[NSNull class]] == NO;
}];

从这里开始,如果需要,您可以使用这些键生成相应的字典。

NSMutableDictionary *nonNullDict = [NSMutableDictionary dictionary];
[d enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
    if ([nonnullKeys contains:key]) {
        nonNullDict[key] = obj;
    }
}];

如果您不需要单独的密钥列表,只需要过滤的字典,请跳过第一步并修改第二部分,如下所示:

NSMutableDictionary *nonNullDict = [NSMutableDictionary dictionary];
[d enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
    if ([obj isKindOfClass:[NSNull null]] == NO) {
        nonNullDict[key] = obj;
    }
}];

答案 1 :(得分:1)

在NSDictionary上写类别它会为你提供null free dictionary。这是我为自己写的类别。

.h文件(接口)的代码

#import <Foundation/Foundation.h>

@interface NSDictionary (CheckNull)
{

}

- (NSDictionary *)nullFreeDictionary;

@end

.m文件的代码。 (实现)

#import "NSDictionary+CheckNull.h"

@implementation NSDictionary (CheckNull)
- (NSDictionary *) nullFreeDictionary
{
    NSMutableDictionary *tempDictionary = [self mutableCopy];
    for (NSString *key in tempDictionary.allKeys) {
        NSString *value = [tempDictionary valueForKey:key];
        if ([value isKindOfClass:[NSString class]]) {
            if (value == (id)[NSNull null] || value == nil || value.length == 0) {
                [tempDictionary setValue:@"" forKey:key];
            }
        }
    }
    return tempDictionary;
}

使用上述类别在您的字典上调用null free方法。

  NSDictionary *dict = [dict nullFreeDictionary];

答案 2 :(得分:-1)

//从Dictionary中删除NULL

-(NSMutableDictionary *)removeNullFromDictionary : (NSMutableDictionary *)dict
{
    //    if (![dict isKindOfClass:[NSMutableDictionary class]])
    //    {
    //    }
    dict = [[NSMutableDictionary alloc] initWithDictionary:dict];

    for (NSString * key in [dict allKeys])
    {
        if ([dict[key] isKindOfClass:[NSNull class]])
        {
            [dict setValue:@"" forKey:key];
        }
        else if ([dict[key] isKindOfClass:[NSMutableDictionary class]]||[dict[key] isKindOfClass:[NSDictionary class]])
        {
            dict[key] = [self removeNullFromDictionary:[NSMutableDictionary dictionaryWithDictionary:dict[key]]];
        }
        else if ([dict[key] isKindOfClass:[NSMutableArray class]]||[dict[key] isKindOfClass:[NSArray class]])
        {
            dict[key] = [self removeNullFromArray:[NSMutableArray arrayWithArray:dict[key]]];
        }
    }

    return dict;
}

//从数组中删除NULL

-(NSMutableArray *)removeNullFromArray : (NSMutableArray *)arr
{
    //    if (![arr respondsToSelector:@selector(addObject:)])
    //    {
    //        arr = [[NSMutableArray alloc] initWithArray:arr];
    //    }
    arr = [[NSMutableArray alloc] initWithArray:arr];
    for (int cnt = 0; cnt<[arr count]; cnt++)
    {
        if ([arr[cnt] isKindOfClass:[NSNull class]])
        {
            arr[cnt] = @"";
        }
        else if ([arr[cnt] isKindOfClass:[NSMutableDictionary class]]||[arr[cnt] isKindOfClass:[NSDictionary class]])
        {
            arr[cnt] = [self removeNullFromDictionary:[NSMutableDictionary dictionaryWithDictionary:arr[cnt]]];
        }
        else if ([arr[cnt] isKindOfClass:[NSMutableArray class]]||[arr[cnt] isKindOfClass:[NSArray class]])
        {
            arr[cnt] = [self removeNullFromArray:[NSMutableArray arrayWithArray:arr[cnt]]];
        }
    }
    return arr;
}