1

NSXMLを使用してXMLドキュメントを解析し、結果をオブジェクトの配列に追加しています。配列には正しい数のオブジェクトがありますが、それらは最後のオブジェクトからのデータでいっぱいです(つまり、インデックス0のオブジェクトはインデックス3のオブジェクトと同じデータを持っています)。サーバーから適切なデータを取得しています。

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

1 に答える 1

2

原因は、addObjects:がcurrentRoleオブジェクトを保持し、そこからコピーを作成しないことです。for内に新しいcurrentRoleオブジェクトを作成するか、そこからコピーを作成して配列に追加することができます。私は次のことをお勧めします:

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
于 2012-11-19T20:52:22.343 に答える