Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:49:05+00:00 2026-06-10T14:49:05+00:00

This question is similar to this question , however this method only works on

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:49:07+00:00Added an answer on June 10, 2026 at 2:49 pm

    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" = "";
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to GWT Table that supports sorting, scrolling and filtering However
The question is similar to this question. However, this one is about exceptions, not
Similar to this question: link However I have already mastered that. My problem is
I have a similar question to this one However, I have a less strict
This is similar to a question that has already been asked. However, I am
This question is similar to this one: Restrict method access to a specific class
This question is similar in concept to this one , except I see I
This question is similar to mine, but does not contain an answer. I want
This question is similar to knockoutjs databind with jquery-ui datepicker , but instead of
This question is similar to this one, but with an extra wrinkle: Auto-removing all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.