将 NSMutableDicitonary 填充到 NSMutableArray 中
我有以下声明:
//.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要确保创建了
dataArray
。将其放入类的init
方法中或其他合适的地方:另外,我希望您的 iPhone 有大量内存,因为您的代码泄漏了相当多的内存。
You need to make sure that
dataArray
is created. Put this in your class'sinit
method or wherever else it would be appropriate:Also, I hope your iPhone has a lot of memory, as your code is leaking a fair bit.
要解决 dreamlax 提到的泄漏问题,您需要在前面添加代码:
使用类似这样的代码:
并且在完成解析后释放它们...
不过,不要检查我的答案是否正确,dreamlax 值得深思熟虑代码...
为了填充 UITableView,您确实需要完成一些 UITableView 示例并了解它们如何加载数据。
To solve the leaks dreamlax mentioned, you need to add code in front of this:
With something like this:
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.
在您的代码中,您有许多
alloc
。这些需要与发布进行平衡。您还有一个需要与发布
平衡的副本
。如果不释放这些对象,最终将一遍又一遍地分配新对象,最终会耗尽内存。您需要做的是找出代码中的合适位置来释放它们。当您不再需要分配的对象时,应该释放它们。对于您的情况,在您的
- (void)parser:didEndElement:namespaceURI:qualifiedName:
方法中添加以下代码可能是明智的,如下所示:请注意,我还添加了
autorelease< /code> 到您的
副本
。copy
很有用,因为它创建了对象的不可变副本,在这种特殊情况下,这可能就是您所追求的。但是,当您创建副本时,您就是该副本的所有者,并且有责任释放它。当您使用addObject:
添加时,您的dataArray
数组将获得您的item
副本的所有权,但您仍然必须通过释放它来放弃您的所有权。但是,我们不能在将其传递给数组之前释放它,否则item
副本将在传递给dataArray
之前被释放,所以它是自动释放的。自动释放的对象保证至少在该方法的剩余时间内存活。自动释放的另一种方法是手动跟踪
item
副本,如下所示:In your code, you have a number of
alloc
s. These need to be balanced withrelease
. You also have acopy
that needs to be balanced with arelease
. 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:Note that I also added an
autorelease
to yourcopy
.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. YourdataArray
array will take ownership of youritem
copy when you add it withaddObject:
, but you must still relinquish your ownership by releasing it. However, we can'trelease
it before we give it to the array, otherwise theitem
copy will be deallocated before it is given todataArray
, 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: