我正在编写一个应用程序,它从服务器中提取 XML 文件,存储它,然后解析它以形成一个UIListView
. 博客文章的名称与其他信息一起存储在 XML 文件中。为了处理数据,我编写了两个类,Post
(存储博客文章的名称、链接、内容等)和PostList
(存储NSMutableArray
所有Post
对象中的一个以及其他信息)。
我遇到了一个问题,在下面的循环中,(getPosts
我的类中的方法的一部分PostList
)每当修改变量时,它都会更改以前循环迭代aPost
的所有存储值的值。aPost
我不明白为什么会这样。谁能解释一下?
TBXMLElement *element = tbxml.rootXMLElement;
TBXMLElement *nextElement = element->nextSibling;
NSLog(@"URLs have been set, preparing for parse/input. [PostList.getPosts]");
//Extracts the content of the XML file and saves it to values in the Post Class
do {
/**********
* This loop goes through the XML file looking for <item> tags that hold information
* about the blog posts. It finds <item> tags and scours them for <title>, <description>,
* <pubdate>, and <link> tags to put into the class variables for the Post Class (aPost).
*********/
NSString *stringElement = [TBXML elementName:element];
NSLog(@"%@", stringElement);
//Sorts through the header junk to find the first <item> tag.
if (![stringElement isEqualToString:@"item"]) {
if (!(element->firstChild)) {
if (!(element->nextSibling)) {
element = nil;
}
element = element->nextSibling;
}
element = element->firstChild;
}
//Once the first <item> tag is found, this code executes.
else {
//Now we move to the first child tag and scour its contents and its siblings
nextElement = [TBXML nextSiblingNamed:@"item" searchFromElement:element];
element = element->firstChild;
do {
//Here it loops over and over until all the parts have been collected.
stringElement = [TBXML elementName:element];
if ([stringElement isEqualToString:@"title"]) {
aPost.name = [TBXML textForElement:element];
}
if ([stringElement isEqualToString:@"description"]) {
aPost.content = [TBXML textForElement:element];
}
if ([stringElement isEqualToString:@"link"]) {
aPost.postURL = [TBXML textForElement:element];
}
if ([stringElement isEqualToString:@"pubdate"]) {
aPost.publicationDate = [TBXML textForElement:element];
}
element = element->nextSibling;
} while (element->nextSibling);
NSLog(@"%@", [self getName:self.aPost]);
[self.postsArray insertObject:aPost atIndex:countingIndex];
countingIndex++;
element = nextElement;
}
} while ((element != nil));