如何检查值 null 并在 NSDictionary 中替换它 [英] How to check value null and replace it in NSDictionary

查看:65
本文介绍了如何检查值 null 并在 NSDictionary 中替换它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在使用 NSUserDefault 和 NSDictionary,我将 NSDictionary 保存在 NSUserDefault 中,不幸的是我不能,因为 NSDictionary 返回到 Json 具有空值.

Now, i'm working with the NSUserDefault and the NSDictionary, i save the NSDictionary in NSUserDefault, unfortunately i can't, because NSDictionary return to Json has null value.

我需要检查 NSDictionary 是否有空值并替换.怎么样?

I need check if NSDictionary has null value and replace. How?

这是 NSDictionary,

It's NSDictionary,

({
  ID:11,
   name:21-12-2012,
   des:"<null>",
   url: 
     [
       {
         ID:1,
         name: "<null>"
       },
       {
         ID:2,
         name:"<null>"
       }
      ]
},
{
   ID:12,
   name:if i die young,
   des:"<null>",
   url: 
     [
       {
         ID:3,
         name: "<null>"
       },
       {
         ID:21,
         name:"<null>"
       }
      ]
})

推荐答案

能否请您查看此链接,我想,它会对您有所帮助,谢谢您的链接替换嵌套 NSDictionary 中出现的 NSNull

could you please check this link, I think, it will be helpful to you, thanks for this link Replace occurrences of NSNull in nested NSDictionary

UPDATE:我稍微修改了一下原文,并使用了一些从这个链接将 nsarray 转换为 nsdictionary 的功能 将 NSArray 转换为 NSDictionary,因为我对您的代码一无所知,所以我尝试使我的 json 字符串尽可能接近您的字符串,并且它的工作,请参阅以下内容.:)

UPDATE: I modified original a bit, and use some the function of converting nsarray to nsdictionary from this link Convert NSArray to NSDictionary, cuz I don't know anything about your code, so I try to make my json string as close as possible to be the same as yours, and it's work, see the following. :)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    tmpDict = [[NSMutableDictionary alloc] init];


    NSMutableDictionary *testDict = [[NSMutableDictionary alloc] init];
    [testDict setValue:[NSNull null] forKey:@"NullValue"];
    [testDict setValue:@"test" forKey:@"UnNull"];
    subArr = [[NSMutableArray alloc] initWithObjects:testDict, testDict, nil];

    [tmpDict setValue:[NSNull null] forKey:@"NullHere"];
    [tmpDict setValue:@"wear" forKey:@"NotNull"];
    [tmpDict setObject:subArr forKey:@"Array"];

    myArr = [[NSMutableArray alloc] initWithObjects:tmpDict, tmpDict, nil];
    NSLog(@"struct: %@", myArr);

    [self dictionaryByReplacingNullsWithStrings];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) dictionaryByReplacingNullsWithStrings {
    const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: [self indexKeyedDictionaryFromArray:myArr]];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in [replaced allKeys]) {
        const id object = [replaced objectForKey: key];
        if (object == nul) {
            [replaced setObject: blank forKey: key];
        }
        else if ([object isKindOfClass: [NSDictionary class]]) {
            NSLog(@"found null inside and key is %@", key);
            [replaced setObject:[self replaceNullInNested:object] forKey:key];
        }

    }
    NSLog(@"replaced: %@", replaced);
}

- (NSMutableDictionary *)replaceNullInNested:(NSDictionary *)targetDict
{
    //make it to be NSMutableDictionary in case that it is nsdictionary
    NSMutableDictionary *m = [targetDict mutableCopy];
    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: m];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in [replaced allKeys]) {
        const id object = [replaced objectForKey: key];
        if (object == nul) {
            [replaced setObject: blank forKey: key];
        }
        else if ([object isKindOfClass: [NSArray class]]) {
            NSLog(@"found null inside and key is %@", key);
            //make it to be able to set value by create a new one
            NSMutableArray *a = [object mutableCopy];
            for (int i =0; i< [a count]; i++) {

                for (NSString *subKey in [[a objectAtIndex:i] allKeys]) {
//                    NSLog(@"key: %@", subKey);
//                    NSLog(@"value: %@", [[object objectAtIndex:i] valueForKey:subKey]);
                    if ([[object objectAtIndex:i] valueForKey:subKey] == nul) {
                        [[object objectAtIndex:i] setValue:blank forKey:subKey];
                    }
                }

            }
            //replace the updated one with old one
            [replaced setObject:a forKey:key];

        }

    }

    return replaced;
}

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
    id objectInstance;
    NSUInteger indexKey = 0;

    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
    for (objectInstance in array)
        [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];

    return (NSDictionary *)mutableDictionary;
}

从上面的代码,这是结果:

from the code above, this is the result:

replacenullvalue[1590:11303] struct: (
        {
        Array =         (
                        {
                NullValue = "<null>";
                UnNull = test;
            },
                        {
                NullValue = "<null>";
                UnNull = test;
            }
        );
        NotNull = wear;
        NullHere = "<null>";
    },
        {
        Array =         (
                        {
                NullValue = "<null>";
                UnNull = test;
            },
                        {
                NullValue = "<null>";
                UnNull = test;
            }
        );
        NotNull = wear;
        NullHere = "<null>";
    }
)
2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is 0
2012-12-16 15:16:22.790 replacenullvalue[1590:11303] found null inside and key is Array
2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is 1
2012-12-16 15:16:22.791 replacenullvalue[1590:11303] found null inside and key is Array
2012-12-16 15:16:22.792 replacenullvalue[1590:11303] replaced: {
    0 =     {
        Array =         (
                        {
                NullValue = "";
                UnNull = test;
            },
                        {
                NullValue = "";
                UnNull = test;
            }
        );
        NotNull = wear;
        NullHere = "";
    };
    1 =     {
        Array =         (
                        {
                NullValue = "";
                UnNull = test;
            },
                        {
                NullValue = "";
                UnNull = test;
            }
        );
        NotNull = wear;
        NullHere = "";
    };
}

希望对你有帮助:)

这篇关于如何检查值 null 并在 NSDictionary 中替换它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆