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 8987683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:51:19+00:00 2026-06-15T21:51:19+00:00

Now, i’m working with the NSUserDefault and the NSDictionary, i save the NSDictionary in

  • 0

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>"
       }
      ]
})
  • 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-15T21:51:20+00:00Added an answer on June 15, 2026 at 9:51 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I know you can't directly compare NULL to anything (as null is unknown)
now i'm working on a project for creating audio unit instrument that provide the
I have a jquery bug and I've been looking for hours now, I can't
Now, this may be really stupid. But can we deploy a Java Swing Application
Now that I'm working in F#, is it time to start writing up some
Now unfortunately due to the fact that WinCE Usb Device Arrival / Removal exposes
Now i face a problem in my porting job, when i need to implement
Now that I have a widescreen monitor, I can't seem to find a way
Now that my facebook app has got more than 1000 likes -- https://apps.facebook.com/156485447732146/ But
Now am working for iPhone/iPad applications. Am interesting to develop apps for Mac OS

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.