heaven heaven - 4 months ago 24
iOS Question

How to check value null and replace it in NSDictionary

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.

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

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>"
}
]
})

Answer

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

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 = "";
    };
}

hope it help you :).

Comments