0

我正在编写一个应用程序,它从服务器中提取 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));
4

2 回答 2

3

不要将 aPost 定义为头文件中的属性,只需在进入循环的 do 部分后直接定义它,例如:

...
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 {
        Post* aPost = [[Post alloc] init]; // <-- create a new post, don't reuse or class level pointer
        //Here it loops over and over until all the parts have been collected.
        stringElement = [TBXML elementName:element];

...并删除头文件中的 Post 属性。

于 2013-04-10T07:20:56.770 回答
2

您应该在外观的每次迭代中创建一个新的 Post 元素,否则数组中的所有条目将引用相同的对象,您将值更新到该对象。

do {
    //...
    if (...) {
    } else {
        //...
        aPost = [[Post alloc] init];
        do {
            //...
        } while(...)
        //...
    }
    //...
} while(...)
于 2013-04-10T07:15:56.073 回答