Replace occurrences of NSNull in nested NSDictionary
Asked Answered
F

8

9

This question is similar to this question, however this method only works on the root level of the dictionary.

I'm looking to replace any occurrence of NSNull values with an empty string, so that I can save the full dictionary to a plist file (if i add it with the NSNull's the file won't write).

My dictionary, however, has nested dictionaries inside it. Like this:

"dictKeyName" = {
    innerStrKeyName = "This is a string in a dictionary";
    innerNullKeyName = "<null>";
    innerDictKeyName = {
        "innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary";
        "innerDictNullKeyName" = "<null>";
    };
};

If I use:

@interface NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

- (NSDictionary *) dictionaryByReplacingNullsWithStrings {

    const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for(NSString *key in replaced) {
        const id object = [self objectForKey:key];
        if(object == nul) {
            [replaced setObject:blank forKey:key];
        }
    }
    return [NSDictionary dictionaryWithDictionary:replaced];
}

@end

I get something like this:

"dictKeyName" = {
    innerStrKeyName = "This is a string in a dictionary";
    innerNullKeyName = ""; <-- this value has changed
    innerDictKeyName = {
        "innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary";
        "innerDictNullKeyName" = "<null>"; <-- this value hasn't changed
    };
};

Is there a way of finding every NSNull value from all dictionaries including nested dictionaries...?

EDIT: The data is being drawn from a JSON feed, so the data I receive is dynamic (and I don't want to have to update the app everytime the feed changes).

Fungiform answered 31/8, 2012 at 10:49 Comment(6)
Also you really should post actual code "<null>" <-- thats neither an NSString or a nullGreece
That is the actual response i'm getting. I'm parsing a JSON Feed and the dictionary is the response. When I log it out, it comes out as "<null>" and is recognised in the find and replace for loop as [NSNull null].Fungiform
The code you have is to manipulate NSDictionary so when you parse your JSON call the method to remove the NSNullsGreece
Do you have any examples of how to do this?Fungiform
Are you still having an issue with this? Did any of the answers resolve the problem?Thermopile
Nope, still the same issues...Fungiform
M
17

A small modification to the method can make it recursive:

@interface NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

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

    for (NSString *key in self) {
        const id object = [self objectForKey: key];
        if (object == nul) {
            [replaced setObject: blank forKey: key];
        }
        else if ([object isKindOfClass: [NSDictionary class]]) {
            [replaced setObject: [(NSDictionary *) object dictionaryByReplacingNullsWithStrings] forKey: key];
        }
    }
    return [NSDictionary dictionaryWithDictionary: replaced];
}

Note that the fast-enumeration is now on self instead of replaced

With the code above, this example:

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
[dic1 setObject: @"string 1" forKey: @"key1.1"];
[dic1 setObject: [NSNull null] forKey: @"key1.2"];

NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
[dic2 setObject: @"string 2" forKey: @"key2.1"];
[dic2 setObject: [NSNull null] forKey: @"key2.2"];

[dic1 setObject: dic2 forKey: @"key1.3"];

NSLog(@"%@", dic1);
NSLog(@"%@", [dic1 dictionaryByReplacingNullsWithStrings]);

renders this result:

2012-09-01 08:30:16.210 Test[57731:c07] {
    "key1.1" = "string 1";
    "key1.2" = "<null>";
    "key1.3" =     {
        "key2.1" = "string 2";
        "key2.2" = "<null>";
    };
}
2012-09-01 08:30:16.212 Test[57731:c07] {
    "key1.1" = "string 1";
    "key1.2" = "";
    "key1.3" =     {
        "key2.1" = "string 2";
        "key2.2" = "";
    };
Middlebrooks answered 31/8, 2012 at 11:15 Comment(6)
Really? I've added a usage-example with its output.Middlebrooks
The last line is superfluous; it would suffice to return replaced; instead.Recidivate
What if the object is NSArray?Lissie
How to handle in nested json strin, see in this post #48010083Indented
I'd use for (NSString *key in [self allKeys]) even if the current implementation works - because it's both less readable, and you need to dig down a lot to know that fast-enumeration over NSDictionary enumerates keys. (could be values, or pairs, or whatever.Fasano
Also - there are faster enumeration mechanisms for NSDictionary, that are concurrent, and also employ background queues, like [replaced enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull key, NSString* _Nonnull value, BOOL * _Nonnull stop) { if (value == [NSNull null]) [self setValue:@"N/A" forKey:key]; }];Fasano
F
10

It works for me, I used nested looping to replace all NULL with nil in the entire dictionary including NSArray.

- (NSDictionary *) dictionaryByReplacingNullsWithNil:(NSDictionary*)sourceDictionary {

    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:sourceDictionary];
    const id nul = [NSNull null];

    for(NSString *key in replaced) {
        const id object = [sourceDictionary objectForKey:key];
        if(object == nul) {
            [replaced setValue:nil forKey:key];
        }
    }
    return [NSDictionary dictionaryWithDictionary:replaced];
}

-(NSDictionary *) nestedDictionaryByReplacingNullsWithNil:(NSDictionary*)sourceDictionary
{
    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:sourceDictionary];
    const id nul = [NSNull null];
    const NSString *blank = @"";
    [sourceDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
        object = [sourceDictionary objectForKey:key];
        if([object isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *innerDict = object;
            [replaced setObject:[self nestedDictionaryByReplacingNullsWithNil:innerDict] forKey:key];

        }
        else if([object isKindOfClass:[NSArray class]]){
            NSMutableArray *nullFreeRecords = [NSMutableArray array];
            for (id record in object) {

                if([record isKindOfClass:[NSDictionary class]])
                {
                    NSDictionary *nullFreeRecord = [self nestedDictionaryByReplacingNullsWithNil:record];
                    [nullFreeRecords addObject:nullFreeRecord];
                }
            }
            [replaced setObject:nullFreeRecords forKey:key];
        }
        else
        {
            if(object == nul) {
                [replaced setObject:blank forKey:key];
            }
        }
    }];

    return [NSDictionary dictionaryWithDictionary:replaced];  
}
Foursquare answered 21/3, 2013 at 8:4 Comment(3)
this implementation is much useful than the accepted one cause it takes arrays into accountNoleta
This implementation won't take into account arrays, which contain non-dictionary data (e.g. strings or arrays). It will return an empty arrayMesentery
Excuse me... but [someMutableDictionary setValue:nil forKey @"someKey"] does NOT introduce a nil into the dictionary- that would crash immediately. Instead it REMOVES the key/value pair altogether. I'm not sure this is good as answer to the OP needsFasano
J
2

In case anyone needs this for swift 1.2, here's the snippet:

class func removeNullsFromDictionary(origin:[String:AnyObject]) -> [String:AnyObject] {
    var destination:[String:AnyObject] = [:]
    for key in origin.keys {
        if origin[key] != nil && !(origin[key] is NSNull){
            if origin[key] is [String:AnyObject] {
                destination[key] = self.removeNullsFromDictionary(origin[key] as! [String:AnyObject])
            } else if origin[key] is [AnyObject] {
                let orgArray = origin[key] as! [AnyObject]
                var destArray: [AnyObject] = []
                for item in orgArray {
                    if item is [String:AnyObject] {
                        destArray.append(self.removeNullsFromDictionary(item as! [String:AnyObject]))
                    } else {
                        destArray.append(item)
                    }
                }
                destination[key] = destArray
            } else {
                destination[key] = origin[key]
            }
        } else {
            destination[key] = ""
        }
    }
    return destination
}
Juna answered 31/8, 2015 at 19:51 Comment(2)
works, but you forgot the destination[key] = destArray at end of cycleFertilizer
@Danilo, Thanks for the comment, i edited the snippetJuna
A
2

Following method works perfectly for any number of nested arrray of dictionary:

- (NSMutableDictionary *)dictionaryByReplacingNullsWithStrings:(NSDictionary *)jobList
{
    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:jobList];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in [replaced allKeys])
    {
        id object = [replaced objectForKey:key];
        if (object == nul)
        {
            [replaced setObject:blank
                         forKey:key];
        }
        else
        if ([object isKindOfClass:[NSDictionary class]])
        {
            [replaced setObject:[self replaceNullInNested:object]
                         forKey:key];
        }
        else
        if ([object isKindOfClass:[NSArray class]])
        {
            NSMutableArray *dc = [[NSMutableArray alloc] init];
            for (NSDictionary *tempDict in object)
            {
                [dc addObject:[self dictionaryByReplacingNullsWithStrings:tempDict]];
            }
            [replaced setObject:dc
                         forKey:key];
        }
    }
    return 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])
                {
                    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;
}

I used above modified method as per my required functionality:

// Call Method

NSMutableDictionary *sortedDict = [[NSMutableDictionary alloc] init];

for (NSString *key in jobList){
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

    for (NSDictionary *tempDict in [jobList objectForKey:key])
    {
        [tempArray addObject:[self dictionaryByReplacingNullsWithStrings:tempDict]];
    }
    [sortedDict setObject:tempArray forKey:key];
}
Abingdon answered 2/11, 2015 at 11:52 Comment(0)
G
0

This code

@interface NSDictionary (JRAdditions)
    - (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end

Monkey patches NSDictionary - which means you can call dictionaryByReplacing... On not just the root but any nested dictionaries as you please.

I dont really agree with this from a design standpoint but it does solve your problem.

Greece answered 31/8, 2012 at 11:3 Comment(2)
I know this, but I the data is dynamic, so I don't want to have to re-release the code every time the dictionary is updated.Fungiform
Check if object is a dictionary and replace with with result of calling method.Thermopile
M
0

try this:

@interface NSDictionary (JRAdditions)
 - (NSDictionary *) dictionaryByReplacingNullsWithStrings;
 -(NSDictionary *) nestedDictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

- (NSDictionary *) dictionaryByReplacingNullsWithStrings {

const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self];
const id nul = [NSNull null];
const NSString *blank = @"";

for(NSString *key in replaced) {
    const id object = [self objectForKey:key];
    if(object == nul) {
        [replaced setObject:blank forKey:key];
    }
}
return [NSDictionary dictionaryWithDictionary:replaced];
}

-(NSDictionary *) nestedDictionaryByReplacingNullsWithStrings
{  
  const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self];
  const id nul = [NSNull null];
  const NSString *blank = @"";
  for(id *item in replaced) {
    const id object = [self objectForKey:key];
    if([object isKindofClass:[NSDictionary class]])
    {
       NSDictionary *innerDict = object;
       [replaced setObject:[innerDict dictionaryByReplacingNullsWithStrings] forKey:key];

    }
    else
    {
     if(object == nul) {
        [replaced setObject:blank forKey:key];
     }
    }
 }
 return [NSDictionary dictionaryWithDictionary:replaced];  
}
Mallon answered 31/8, 2012 at 11:30 Comment(0)
V
0

This solution works with arrays and dictionaries, and also for nested arrays and dictionaries etc (recursively).

- (NSDictionary *)writableDictionary:(NSDictionary *)dictionary
{
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];

    for (id key in mutableDictionary.allKeys)
    {
        id value = mutableDictionary[key];
        mutableDictionary[key] = [self writableValue:value];
    }

    return mutableDictionary;
}

- (NSArray *)writableArray:(NSArray *)array
{
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];

    for (int i = 0; i < mutableArray.count; ++i)
    {
        id value = mutableArray[i];
        mutableArray[i] = [self writableValue:value];
    }

    return mutableArray;
}

- (id)writableValue:(id)value
{
    if ([value isKindOfClass:[NSNull class]])
    {
        value = @"";
    }
    else if ([value isKindOfClass:[NSDictionary class]])
    {
        value = [self writableDictionary:value];
    }
    else if ([value isKindOfClass:[NSArray class]])
    {
        value = [self writableArray:value];
    }
    return value;
}
Vange answered 24/10, 2014 at 13:23 Comment(0)
C
0

The above mentioned answers does not cater the situation if you have array in your dictionary. Check this out

+(NSMutableDictionary*)getValuesWithOutNull:(NSDictionary          
     *)yourDictionary{
       NSMutableDictionary *replaced = [NSMutableDictionary 
     dictionaryWithDictionary: yourDictionary];
      id nul = [NSNull null];
      NSString *blank = @"";

for (NSString *key in yourDictionary) {
    const id object = [yourDictionary objectForKey: key];
    if (object == nul) {
        [replaced setObject: blank forKey: key];
    }
    else if ([object isKindOfClass: [NSDictionary class]]) {
        [replaced setObject:[self getValuesWithOutNull:object] 
forKey:key];
    }
    else if([object isKindOfClass: [NSArray class]])
    {
        NSMutableArray *array  = [NSMutableArray 
arrayWithArray:object];
        for(int i = 0 ;i < array.count;i++)
        {
            NSDictionary *dict = [array objectAtIndex:i];
            [array replaceObjectAtIndex:i withObject:[self 
  getValuesWithOutNull:dict]];
        }
        [replaced setObject:array forKey:key];
    }
 }
  return  replaced;

}
Colcannon answered 22/7, 2015 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.