解析XML时,数组被覆盖(Objective-C)

Jos*_*Jos 0 iphone memory-management objective-c ios tbxml

我正在使用TBXML解析器,但遇到了一些困难.

我使用以下代码将产品对象中的数据添加到名为laarzen的MutableArray中.

- (void)loadURL {
    // Load and parse an xml string
    Products *product = [[Products alloc] init];
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"url..."]];

    // If TBXML found a root node, process element and iterate all children

    // Obtain root element
    TBXMLElement * root = tbxml.rootXMLElement;

    // if root element is valid
    if (root) {
        // search for the first category element within the root element's children

        TBXMLElement * Category = [TBXML childElementNamed:@"Category" parentElement:root];
        Products *product = [[Products alloc] init];
        // if an author element was found
        while (Category!= nil) {

            // instantiate an author object


            // get the name attribute from the author element
            product.category = [TBXML valueOfAttributeNamed:@"DisplayName" forElement:Category];

            // search the author's child elements for a book element
            TBXMLElement *xmlProduct = [TBXML childElementNamed:@"Product" parentElement:Category];
            int i;
            i=0;
            // if a book element was found
            while (xmlProduct != nil) {
    // extract the title attribute from the book element
                    product.productId = [TBXML valueOfAttributeNamed:@"Id" forElement:xmlProduct];

                    // extract the title attribute from the book element
                    NSString * price = [TBXML valueOfAttributeNamed:@"Price" forElement:xmlProduct];

                    // if we found a price
                    if (price != nil) {
                        // obtain the price from the book element
                        product.price = [NSNumber numberWithFloat:[price floatValue]];
                    }



                    // add the book object to the author's books array and release the resource
                    [laarzen addObject:product];

// find the next sibling element named "book"
                xmlProduct = [TBXML nextSiblingNamed:@"Product" searchFromElement:xmlProduct];
            }

            // add our author object to the authors array and release the resource


            // find the next sibling element named "author"
            Category = [TBXML nextSiblingNamed:@"Category" searchFromElement:Category];
        }

    }


    // release resources
    [tbxml release];
    [product release];
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,从xml解析的最后一个节点会覆盖数组中的所有条目.即如果最后一个价格是39,99,那么阵列中的价格将是39,99.

在我的viewdidload中,我按如下方式分配laarzen:

laarzen = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)

在我的dealloc我再次释放它.

在头文件中,我执行了以下操作:

NSMutableArray *laarzen;
@property (nonatomic, retain) NSMutableArray *laarzen;
Run Code Online (Sandbox Code Playgroud)

它可能是一个内存问题,但我只是没有看到它.

日Thnx!

Ken*_*ner 5

除非您每次都在制作新的产品对象,否则您只需在阵列中存储大量对同一产品对象的引用 - 因此价格似乎相同,因为它们都是相同的产品.