它正在使用ElementParser库从RSS提要中创建一个值的字典(除了我省略的标题之外还有其他值)。 self.articles 是一个NSMutableArray,它将所有的字典存储在RSS文档中。
最后,这应该产生一个字典数组,每个字典包含我需要关于任何数组索引的项目的信息。当我尝试使用 setValue:forKey:它给我
这个类是不符合键值标题的键值编码
错误。这与Interface Builder无关,它只是代码。为什么我得到这个错误?
解决方案首先,你使用 -setValue:forKey:您应该使用 -setObject:forKey:在字典上的。其次,你试图突破一个 NSDictionary ,这是一个不可变的对象,而不是一个 NSMutableDictionary 工作。如果您切换到使用 -setObject:forKey:,则可能会收到异常,告诉您该字典是不可变的。将文章初始化切换到
NSMutableDictionary * article = [[NSMutableDictionary alloc] init];它应该工作。
I have this simple loop in my program:
for (Element *e in items) { NSDictionary *article = [[NSDictionary alloc] init]; NSLog([[e selectElement: @"title"] contentsText]); [article setValue: [[e selectElement: @"title"] contentsText] forKey: @"Title"]; [self.articles insertObject: article atIndex: [self.articles count]]; [article release]; }It is using the ElementParser library to make a dictionary of values from an RSS feed (there are other values besides "title" which I have omitted). self.articles is an NSMutableArray which is storing all of the dictionaries in the RSS document.
In the end, this should produce an array of dictionaries, with each dictionary containing the information I need about the item at any array index. When I try to use setValue:forKey: it gives me the
this class is not key value coding-compliant for the key "Title"
error. This has nothing to do with Interface Builder, it is all code-only. Why am I getting this error?
解决方案First off, you're using -setValue:forKey: on a dictionary when you should be using -setObject:forKey:. Secondly, you're trying to mutate an NSDictionary, which is an immutable object, instead of an NSMutableDictionary, which would work. If you switch to using -setObject:forKey: you'll probably get an exception telling you that the dictionary is immutable. Switch your article initialization over to
NSMutableDictionary *article = [[NSMutableDictionary alloc] init];and it should work.
更多推荐
NSDictionary setValue:forKey:
发布评论