将 NSMutableDicitonary 填充到 NSMutableArray 中

发布于 2024-08-17 11:48:07 字数 1813 浏览 8 评论 0原文

我有以下声明:

 //.h file:
    NSMutableArray *dataArray;
    NSMutableDictionary *item;

    //.m file
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
        currentElement = [elementName copy];
        if ([elementName isEqualToString:@"table"]){
        item = [[NSMutableDictionary alloc] init];
        currentEid = [[NSMutableString alloc] init];
        currentEname = [[NSMutableString alloc] init];
        currentEurl = [[NSMutableString alloc] init];
        }
        //NSLog(@"didStartElement");
        }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     

        if ([elementName isEqualToString:@"table"]) {
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            [self.dataArray addObject:[item copy]];
            NSLog(@"%@",dataArray);
            }
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        if ([currentElement isEqualToString:@"eid"]) {
            [currentEid appendString:string];
        } else if ([currentElement isEqualToString:@"ename"]) {
            [currentEname appendString:string];
            //NSLog(@"%@",currentEname);
        } else if ([currentElement isEqualToString:@"eurl"]) {
            [currentEurl appendString:string];
        } 
        //  NSLog(@"foundCharacters");
    }

显示时,我得到 NSMutableArray(dataArray) 的空值。我该如何解决这个问题? 或者,如果可以的话,请帮助我了解如何在表视图单元格中显示 NSMutableDicitonary 数据。

I have following declarations:

 //.h file:
    NSMutableArray *dataArray;
    NSMutableDictionary *item;

    //.m file
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
        currentElement = [elementName copy];
        if ([elementName isEqualToString:@"table"]){
        item = [[NSMutableDictionary alloc] init];
        currentEid = [[NSMutableString alloc] init];
        currentEname = [[NSMutableString alloc] init];
        currentEurl = [[NSMutableString alloc] init];
        }
        //NSLog(@"didStartElement");
        }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     

        if ([elementName isEqualToString:@"table"]) {
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            [self.dataArray addObject:[item copy]];
            NSLog(@"%@",dataArray);
            }
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        if ([currentElement isEqualToString:@"eid"]) {
            [currentEid appendString:string];
        } else if ([currentElement isEqualToString:@"ename"]) {
            [currentEname appendString:string];
            //NSLog(@"%@",currentEname);
        } else if ([currentElement isEqualToString:@"eurl"]) {
            [currentEurl appendString:string];
        } 
        //  NSLog(@"foundCharacters");
    }

I get null values for the NSMutableArray(dataArray) when displayed. How could I solve this?
Or if you could, assist me with knowledge on how to display the NSMutableDicitonary data in table view cells.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼睛不忧郁 2024-08-24 11:48:07

您需要确保创建了dataArray。将其放入类的 init 方法中或其他合适的地方:

dataArray = [[NSMutableArray alloc] init];

另外,我希望您的 iPhone 有大量内存,因为您的代码泄漏了相当多的内存。

You need to make sure that dataArray is created. Put this in your class's init method or wherever else it would be appropriate:

dataArray = [[NSMutableArray alloc] init];

Also, I hope your iPhone has a lot of memory, as your code is leaking a fair bit.

挽袖吟 2024-08-24 11:48:07

要解决 dreamlax 提到的泄漏问题,您需要在前面添加代码:

item = [[NSMutableDictionary alloc] init];
        currentEid = [[NSMutableString alloc] init];
        currentEname = [[NSMutableString alloc] init];
        currentEurl = [[NSMutableString alloc] init];

使用类似这样的代码:

[item release]; [currentEid release]; [currentEname release]; [currentEurl release];
item = [[NSMutableDictionary alloc] init];
            currentEid = [[NSMutableString alloc] init];
            currentEname = [[NSMutableString alloc] init];
            currentEurl = [[NSMutableString alloc] init];

并且在完成解析后释放它们...

不过,不要检查我的答案是否正确,dreamlax 值得深思熟虑代码...

为了填充 UITableView,您确实需要完成一些 UITableView 示例并了解它们如何加载数据。

To solve the leaks dreamlax mentioned, you need to add code in front of this:

item = [[NSMutableDictionary alloc] init];
        currentEid = [[NSMutableString alloc] init];
        currentEname = [[NSMutableString alloc] init];
        currentEurl = [[NSMutableString alloc] init];

With something like this:

[item release]; [currentEid release]; [currentEname release]; [currentEurl release];
item = [[NSMutableDictionary alloc] init];
            currentEid = [[NSMutableString alloc] init];
            currentEname = [[NSMutableString alloc] init];
            currentEurl = [[NSMutableString alloc] init];

And also reelase them when you are done parsing...

Don't check my answer as correct though, dreamlax deserves the credit for pondering the code...

For populating a UITableView, you really need to work through some of the UITableView examples and see how they load data.

素食主义者 2024-08-24 11:48:07

在您的代码中,您有许多 alloc。这些需要与发布进行平衡。您还有一个需要与发布平衡的副本。如果不释放这些对象,最终将一遍又一遍地分配新对象,最终会耗尽内存。

您需要做的是找出代码中的合适位置来释放它们。当您不再需要分配的对象时,应该释放它们。对于您的情况,在您的 - (void)parser:didEndElement:namespaceURI:qualifiedName: 方法中添加以下代码可能是明智的,如下所示:


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"table"]) {
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            [self.dataArray addObject:[[item copy] autorelease]];
            NSLog(@"%@",dataArray);
            }
        [currentEid release];
        [currentEname release];
        [currentEurl release];
    }

请注意,我还添加了 autorelease< /code> 到您的副本copy 很有用,因为它创建了对象的不可变副本,在这种特殊情况下,这可能就是您所追求的。但是,当您创建副本时,您就是该副本的所有者,并且有责任释放它。当您使用 addObject: 添加时,您的 dataArray 数组将获得您的 item 副本的所有权,但您仍然必须通过释放它来放弃您的所有权。但是,我们不能在将其传递给数组之前释放它,否则 item 副本将在传递给 dataArray 之前被释放,所以它是自动释放的。自动释放的对象保证至少在该方法的剩余时间内存活。

自动释放的另一种方法是手动跟踪 item 副本,如下所示:


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"table"]) {
            NSDictionary *itemCopy;
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            itemCopy = [item copy];
            [self.dataArray addObject:itemCopy];
            [itemCopy release];
            NSLog(@"%@",dataArray);
            }
        [currentEid release];
        [currentEname release];
        [currentEurl release];
    }

In your code, you have a number of allocs. These need to be balanced with release. You also have a copy that needs to be balanced with a release. If you don't release these objects, you will end up allocating new objects over and over, and you will eventually run out of memory.

What you need to do is figure out where in your code is a suitable place to release them. When you no longer need the objects that you allocated, you should release them. In your case, it may be sensible to add the following code in your - (void)parser:didEndElement:namespaceURI:qualifiedName: method, like this:


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"table"]) {
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            [self.dataArray addObject:[[item copy] autorelease]];
            NSLog(@"%@",dataArray);
            }
        [currentEid release];
        [currentEname release];
        [currentEurl release];
    }

Note that I also added an autorelease to your copy. copy useful because it creates an immutable copy of your object, which in this particular case might be what you're after. However, when you create a copy, you are the owner of the copy and you are responsible for releasing it. Your dataArray array will take ownership of your item copy when you add it with addObject:, but you must still relinquish your ownership by releasing it. However, we can't release it before we give it to the array, otherwise the item copy will be deallocated before it is given to dataArray, so it is auto-released instead. An autoreleased object is guaranteed to survive for at least the remainder of the method.

An alternative to autoreleasing is to manually keep track of the item copy, like this:


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
        if ([elementName isEqualToString:@"table"]) {
            NSDictionary *itemCopy;
            // save values to an item, then store that item into the array...
            [item setObject:currentEname forKey:@"ename"];
            //NSLog(@"%@",item);
            itemCopy = [item copy];
            [self.dataArray addObject:itemCopy];
            [itemCopy release];
            NSLog(@"%@",dataArray);
            }
        [currentEid release];
        [currentEname release];
        [currentEurl release];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文