Вопрос

I am using NSXML to parse out an XML document and add the results to an array of objects. The array has the correct number of objects, but they are full of data from the last object.(i.e. the object at index 0 has the same data as at index 3). I am getting good data back from my server.

//set up my objects and arrays higher in my structure
SignatureResult *currentSignatureResult = [[SignatureResult alloc]init];
Document *currentDoc = [[Document alloc]init];
Role *currentRole = [[Role alloc]init];             
NSMutableArray *roleArray = [[NSMutableArray alloc] init];
NSMutableArray *doclistArray2 = [[NSMutableArray alloc] init];


.....there is more parsing up here
//role is defined as an NSXML Element
for (role in [roleList childrenNamed:@"role"]){

    NSString *firstName =[role valueWithPath:@"firstName"];
    NSString *lastName = [role valueWithPath:@"lastName"];
    currentRole.name = [NSString stringWithFormat:@"%@ %@",firstName, lastName];



    for (documentList2 in [role childrenNamed:@"documentList"])
       {
        SMXMLElement *document = [documentList2 childNamed:@"document"];
        currentDoc.name = [document attributeNamed:@"name"];
        [doclistArray2 addObject:currentDoc];
        }
    currentRole.documentList = doclistArray2;
    [roleArray addObject:currentRole];
    ///I've logged currentRole.name here and it shows the right information

}//end of second for statemnt

currentSignatureResult.roleList = roleArray;
}
///when I log my array here, it has the correct number of objects, but each is full of
///data from the last object I parsed
Это было полезно?

Решение

The cause is that the addObjects: retains for your currentRole object and not creates a copy from that. You can create your new currentRole object inside of the for or you can create a copy from that and add it to the array. I recommend the following:

for (role in [roleList childrenNamed:@"role"]){
    Role *currentRole = [[Role alloc] init];
    NSString *firstName =[role valueWithPath:@"firstName"];
    NSString *lastName = [role valueWithPath:@"lastName"];
    currentRole.name = [NSString stringWithFormat:@"%@ %@",firstName, lastName];



    for (documentList2 in [role childrenNamed:@"documentList"])
       {
        SMXMLElement *document = [documentList2 childNamed:@"document"];
        currentDoc.name = [document attributeNamed:@"name"];
        [doclistArray2 addObject:currentDoc];
        }
    currentRole.documentList = doclistArray2;
    [roleArray addObject:currentRole];
    ///I've logged currentRole.name here and it shows the right information
    [currentRole release];

}//end of second for statemnt
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top