Sudzc deserializeAsDictionary: over written dictionary

后端 未结 5 986
渐次进展
渐次进展 2021-01-17 08:06

The Sudzc generated code is over writing a dictionary for deserialized nodes. If I use the NSLog(@\"The Child Node: %@\", [[[element children] objectAtIndex:0] stringValue]

相关标签:
5条回答
  • 2021-01-17 08:41

    I'm uncertain on how you applied your fix, are you replacing the entire code in deserializeAsDictionary or are you appending to the end of the code?

    see there's a for loop where the code line

      [d setObject:v forKey:[child name]];
    

    is found, so I'm guessing you simply extended that so that instead of closing the for loop you simply extend it here, is this right?

    0 讨论(0)
  • 2021-01-17 08:46

    Solved (changed the soap.m):

    [d setObject:v forKey:[child name]]; 
    NSString* key = [child name]; 
    id check = [d objectForKey:key]; 
    if( check != nil ) { 
    
        NSInteger next = 1; 
        key = [NSString stringWithFormat:@"%@%d", [child name], next]; 
        check = [d objectForKey:key]; 
        while( check != nil ) { 
    
            next++; 
            key = [NSString stringWithFormat:@"%@%d", [child name], next]; 
            check = [d objectForKey:key]; 
        } 
        [d setObject:v forKey:key]; 
    } 
    [d setObject:v forKey:[child name]]; 
    
    0 讨论(0)
  • 2021-01-17 08:51

    I found that you have to put an else block in as well:

    if( check != nil ) {             
        NSInteger next = 1;             
        key = [NSString stringWithFormat:@"%@%04d", [child name], next];
        check = [d objectForKey:key]; 
        while( check != nil ) {                 
            next++; 
            key = [NSString stringWithFormat:@"%@%04d", [child name], next]; 
            check = [d objectForKey:key]; 
        } 
        [d setObject:v forKey:key]; 
    } else {
        [d setObject:v forKey:[child name]];
    }
    //End Extension
    

    Otherwise elements in 'd' will be overwritten because setObject is called twice.

    0 讨论(0)
  • 2021-01-17 09:01

    as soon as I get enough rep points I will, but I did notice that the code begins and ends with

      [d setObject:v forKey:[child name]];
    

    and for me I had to remove the initial line, and that fixed it for me, so the code looks like this:

    // Deserializes the element in a dictionary.
     +(id)deserializeAsDictionary:(CXMLNode*)element {
    
    if([element childCount] == 1) {
        CXMLNode* child = [[element children] objectAtIndex:0];
        if([child kind] == CXMLTextKind) {            
        return [[[element children] objectAtIndex:0] stringValue];
        }
    }   
    NSMutableDictionary* d = [NSMutableDictionary dictionary];
    
    for(CXMLNode* child in [element children]) {
    
    id v = [Soap deserialize:child];
    if(v == nil) { v = [NSNull null]; }
    
    //[d setObject:v forKey:[child name]]; //seems to be duped (maybe my bad)
        //Extended by iDev on StackOverflow
        //http://stackoverflow.com/questions/10235496/sudzc-deserializeasdictionary-over-written-dictionary/10358458#10358458
        NSString* key = [child name]; 
        id check = [d objectForKey:key]; 
        if( check != nil ) {             
            NSInteger next = 1;             
            key = [NSString stringWithFormat:@"%@%04d", [child name], next];
            check = [d objectForKey:key]; 
            while( check != nil ) {                 
                next++; 
                key = [NSString stringWithFormat:@"%@%04d", [child name], next]; 
                check = [d objectForKey:key]; 
            } 
            [d setObject:v forKey:key]; 
        } 
        [d setObject:v forKey:[child name]]; 
        //End Extension
    }
    return d;
    

    }

    0 讨论(0)
  • 2021-01-17 09:06

    I was having issues with the above code - it was overwriting the first entry each time - ie I would get a list of 4 items and the first and fourth would be duplicated.

    After a lot of exhausting stepping thru code (love recursive code NOT), I found what I believe was the issue;

    My code is as follows:

        if( check != nil ) {
            NSInteger next = 1;
            key = [NSString stringWithFormat:@"%@%04d", [child name], next];
            check = [d objectForKey:key];
            while( check != nil ) {
                next++;
                key = [NSString stringWithFormat:@"%@%04d", [child name], next];
                check = [d objectForKey:key];
            }
            [d setObject:v forKey:key];  
        }
        else
        {
              [d setObject:v forKey:[child name]];
        }
    
    0 讨论(0)
提交回复
热议问题