Sudzc deserializeAsDictionary:在编写词典(Sudzc deserial

2019-06-23 18:30发布

该Sudzc生成的代码是在编写一本词典的反序列化的节点。 如果我使用的NSLog(@ “子节点:%@”,[[[元素子] objectAtIndex:0] stringValue的]); 它将写入正确的项目它穿过每次出来。 当我尝试检索代码的结果只有最后一个可用(杰克逊3)。 我究竟做错了什么?

// Deserializes the element in a dictionary.
+(id)deserializeAsDictionary:(CXMLNode*)element {

if([element childCount] == 1) {
    CXMLNode* child = [[element children] objectAtIndex:0];
    if([child kind] == CXMLTextKind) 
    {
         NSLog(@"The Child Node: %@", [[[element children] objectAtIndex:0] stringValue]);
        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]];
}
return d;
}

NSLog的:

2012-04-19 14:13:07.802 Management[3043:10703] Hopefully Child: Allen
2012-04-19 14:13:07.803 Management[3043:10703] Hopefully Child: 1
2012-04-19 14:13:07.804 Management[3043:10703] Hopefully Child: John
2012-04-19 14:13:07.804 Management[3043:10703] Hopefully Child: 2
2012-04-19 14:13:07.805 Management[3043:10703] Hopefully Child: Jackson
2012-04-19 14:13:07.805 Management[3043:10703] Hopefully Child: 3

XML:

<TC diffgr:id="TC1" msdata:rowOrder="0">
 <CSHR_POS_NAME>Allen</CSHR_POS_NAME>                            
    <CSHR_NUM>66</CSHR_NUM>
</TC>

<TC diffgr:id="TC2" msdata:rowOrder="1">                                    
  <CSHR_POS_NAME>John</CSHR_POS_NAME>
    <CSHR_NUM>2</CSHR_NUM>
    </TC>

<TC diffgr:id="TC3" msdata:rowOrder="2">
<CSHR_POS_NAME>Jackson</CSHR_POS_NAME>
<CSHR_NUM>3</CSHR_NUM>
</TC>

Answer 1:

解决(改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]]; 


Answer 2:

只要我获得足够的代表处点我会的,但我没有注意到代码的开始和结尾

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

对我来说,我不得不删除初始行,并固定它对于我来说,这样的代码如下所示:

// 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;

}



Answer 3:

我发现,你必须把一个else块,以及:

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

否则,在“d”的元素将被覆盖,因为的setObject被调用了两次。



Answer 4:

我对你如何应用你的补丁,你替换deserializeAsDictionary整个代码不确定或者是你附加到代码的结束?

看到有一个for循环,其中的代码行

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

被发现,所以我猜你简单地扩展,这么说,而不是封闭的循环中,您只需在这里扩展它,这是正确的?



Answer 5:

我有问题与上面的代码 - 这是每次覆盖的第一项 - 即我将获得的4项的列表,并且第一和第四将被复制。

很多用尽步进直通码后(爱递归代码没有),我发现了什么,我相信是问题;

我的代码如下:

    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]];
    }


文章来源: Sudzc deserializeAsDictionary: over written dictionary