简体   繁体   English

使用XML解析器中的值填充NSDictionary

[英]Populating NSDictionary with values from an XML parser

My hobby project is getting over my head! 我的爱好项目让我头疼! I am using an XML parser, which gives me a set of values row.blurb row.url row.keywords etc. 我正在使用XML解析器,它为我提供了一组值row.blurb row.url row.keywords等。

I now need to put them into an NSDictionary to populate a table, but my understanding of dictionaries is letting me down. 我现在需要将它们放入NSDictionary中以填充表格,但是我对字典的了解让我失望了。 I was expecting to be able to say something like 我原本希望能够说些类似的话

[exhibitors addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:
[row.blurb],@"blurb",[row.url],@"url",[row.keywords],"keywords",nil]];

But, as none of you will be at all surprised, I get a crash. 但是,由于你们所有人都不会感到惊讶,所以我崩溃了。 Can anyone help me with the correct way to do this? 谁能帮助我以正确的方式做到这一点?

Thanks. 谢谢。

You are providing a C string as one of the keys -> "keywords" . 您正在提供一个C字符串作为键-> "keywords"

That is not a valid key in an NSDictionary instance. 这不是NSDictionary实例中的有效密钥。 You probably intended it to be @"keywords" . 您可能希望将其作为@"keywords"

To add, you are also leaking memory as you are creating an object but aren't releasing it as you don't have a reference. 要添加的是,创建对象时还会泄漏内存,但是由于没有引用,所以不会释放它。 You can use the convenience method dictionaryWithObjectsAndKeys : to create an autoreleased instance so that you don't leak. 您可以使用便捷方法dictionaryWithObjectsAndKeys :来创建一个自动发布的实例,以免泄漏。

[exhibitors addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:row.blurb, @"blurb", row.url, @"url", row.keywords, @"keywords", nil]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM