NSDictionary setValue:forKey: — getting “this class is not key value coding-compliant for the key”

天大地大妈咪最大 提交于 2019-12-03 04:34:20

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.

This:

NSDictionary *article = [[NSDictionary alloc] init];

means that the dictionary is immutable. If you want to change its contents, create a mutable dictionary instead:

NSMutableDictionary *article = [[NSMutableDictionary alloc] init];

Alternatively, you could create your dictionary as:

NSDictionary *article = [NSDictionary dictionaryWithObject:[[e selectElement: @"title"] contentsText] forKey:@"Title"];

and drop the release at the end of that method.

Also, the canonical method to add/replace objects in a (mutable) dictionary is -setObject:forKey:. Unless you are familiar with Key-Value Coding, I suggest you don’t use -valueForKey: and -setValue:forKey:.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!