4

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>"
       }
      ]
})
heaven
  • 221
  • 4
  • 19
  • please see my update, I try my best:) – piam Dec 15 '12 at 05:58
  • Can i convert json to NSString and replace value null then NSString convert to NSDictionary, i think – heaven Dec 15 '12 at 18:01
  • see my update, I create new nsmutablearray from nsarray, update value, then replace the outdated one with update one – piam Dec 16 '12 at 13:23
  • i put nslog with [object objectAtIndex:i] is NSDictionary ->NSLog(@"1-%@",[object objectAtIndex:i]); and with NSLog(@"2 - %@",object) -> NSArray, with NSDictionary i use to setobject but error. ??? – heaven Dec 16 '12 at 16:53
  • possible duplicate of [Replace occurance of NSNull in nested NSDictionary](http://stackoverflow.com/questions/12213822/replace-occurance-of-nsnull-in-nested-nsdictionary) – jscs Feb 19 '15 at 07:04
  • [Please check the following link](http://stackoverflow.com/questions/12213822/replace-occurance-of-nsnull-in-nested-nsdictionary/33476971#33476971) – Harshal Wani Nov 02 '15 at 12:05

1 Answers1

2

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

Community
  • 1
  • 1
piam
  • 653
  • 6
  • 13
  • In, this if ([object isKindOfClass: [NSDictionary class]]) condition, could you please nslog and check it come into this if condition or not. – piam Dec 15 '12 at 04:47
  • error is:[__NSCFArray dictionaryByReplacingNullsWithStrings]: unrecognized selector sent to instance 0x9c8d3b0 and Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray dictionaryByReplacingNullsWithStrings]: unrecognized selector sent to instance 0x9c8d3b0' – heaven Dec 15 '12 at 05:09
  • With your string json code work ok, when my json string transmission, error at "for (NSString *key in [replaced allKeys])" i don't know why? i think maybe problem at two json of you and me diffirent – heaven Dec 15 '12 at 06:43
  • [[yourDictionary objectAtIndex:0] allKeys] is solved the problem? – piam Dec 15 '12 at 07:51
  • you said that error at "for (NSString *key in [replaced allKeys]), so what is the error say? (my previous comment is my guess) – piam Dec 15 '12 at 08:40
  • Your code works very well, but the 2nd part of the json string(json above part of "url") doesn't replace null – heaven Dec 15 '12 at 17:23
  • i don't know why,i can't do exec loop for with multiple array like json above. – heaven Dec 16 '12 at 04:09
  • have an error when replace url in function: replaceNullInNested-> [[object objectAtIndex:i] setValue:blank forKey:subKey]; error is:[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object, and i changed [[object objectAtIndex:i] setObject:blank forKey:subKey] -> still dosen't work. – heaven Dec 16 '12 at 09:46
  • In function replaceNullInNested, this line ->NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: targetDict]; <- your targetDict is init with "[NSDictionary alloc]" ?, if yes, you should change it to "[NSMutableDictionary alloc]". – piam Dec 16 '12 at 10:06
  • Yes! the targetDict is NSDictionary, i don't understand, i will change what... ? – heaven Dec 16 '12 at 10:13
  • your targetDict, it should be NSMutableDictionary, how you init your targetDict?, see this link , hope it help :) – piam Dec 16 '12 at 10:18
  • when i put nslog at else if ([object isKindOfClass: [NSArray class]]) --> the object is NSCFArray -> right, but error say: like your link above -> why? – heaven Dec 16 '12 at 10:23
  • one question for you: can i save nsstring json above in nsuserdefault and don't need replace value null -> now everything i do to be able to save json in nsuserdefault – heaven Dec 16 '12 at 10:26
  • for your first question, from my understanding, it is NSArray inside your NSDictionary, and for NSDictionary, you cannot change/set value, so it should be NSMutableDictionary. – piam Dec 16 '12 at 10:36
  • yeah you right, it is nsarray inside nsdictionary and when replace the nsarray have null value, error occurs "[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object" – heaven Dec 16 '12 at 10:44
  • umm for another question, after I research, the answer is you cannot save null value to NSUserDefaults. by the way right now it work? – piam Dec 16 '12 at 10:46
  • yep i see, and "[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object" can you help me fix it – heaven Dec 16 '12 at 10:56
  • umm you get json string from web service right? then -> targetDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; ----- when you declare targetDict in header file you should declare similar to this -->@interface ViewController : UIViewController { NSMutableDictionary *targetDict; } – piam Dec 16 '12 at 11:11
  • another way is to convert nsdictionary into nsmutabledictionary like this and use the converted one for targetDict – piam Dec 16 '12 at 11:23
  • i must use objectAtIndex to save blank when value is null – heaven Dec 16 '12 at 11:29
  • have you use to skype, yahoo or something like that – heaven Dec 16 '12 at 11:30
  • see my update, in replaceNullInNested. yeah you can use objectAtIndex, you just create new dictionary, which has a type of NSMutablDictionary, then use this one. – piam Dec 16 '12 at 12:20
  • still error, " [[object objectAtIndex:i] setValue:blank forKey:subKey];", i think system don't understand this object is nsarray, its understand NSDictionary – heaven Dec 16 '12 at 12:25
  • in replaceNullInNested, " nslog(@"%@", object) inside if ([[object objectAtIndex:i] valueForKey:subKey] == nul) " for me, let me see your object. – piam Dec 16 '12 at 12:40
  • object return NSArray, but can't use to setvalue. – heaven Dec 16 '12 at 13:06