简体   繁体   English

setvalue:forkey:不起作用,应用程序崩溃

[英]setvalue:forkey: not working, app is crashing

I am working on an app. 我正在开发一个应用程序。 I tried to set value for particular key of NSDictionary using the following code, 我尝试使用以下代码为NSDictionary的特定键设置值,

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@“Jan Month",@“Feb Month",@“Mar Month",@“Apr Month",@“May Month",@“June Month",@“July Month",@“Aug Month",@“Sep Month",@“Oct Month", nil];

NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];

NSDictionary *testDetails=[NSDictionary dictionaryWithObjects:testArray forKeys:testResult];

[testDetails setValue:@"Fail" forKey:@“Feb Month"];

My app is crashing at fourth line giving error, 我的应用在第四行崩溃,提示错误,

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x1741a2f40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Feb Month.'

I tried solving this by checking, 我尝试通过检查解决此问题,

po [self.testDetails valueForKey:"Feb Month"]
<extracting data from value failed>

Please help me in solving this. 请帮我解决这个问题。

For of all you need to set testResult for objects and testArray for keys. 对于所有这些,您需要为对象设置testResult ,为键设置testArray Now if youIf you want to change the value of it you need to declare it as NSMutableDictionary . 现在,如果要更改其值,则需要将其声明为NSMutableDictionary

NSMutableDictionary *testDetails = [NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];
[testDetails setValue:@"Fail" forKey:@“Feb Month"];

try this: 尝试这个:

 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:testResult,testArray, nil];

    [dict setObject:@"Fail" forKey:@"Feb Month"];

Did you set object and key correctly? 您是否正确设置了对象和键? also Did you use NSDictionary and NSMutableDictionary correctly? 还您正确使用了NSDictionary和NSMutableDictionary吗?

I checked your below code first. 我首先检查了您的以下代码。

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@"Jan Month",@"Feb Month",@"Mar Month",@"Apr Month",@"May Month",@"June Month",@"July Month",@"Aug Month",@"Sep Month",@"Oct Month", nil];
NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];
NSDictionary *testDetails=[NSDictionary dictionaryWithObjects:testArray forKeys:testResult];
[testDetails setValue:@"Fail" forKey:@"Feb Month"];

Now see the result.There is only 1 key value pair 现在查看结果。只有1个键值对

在此处输入图片说明

What your mistake here is you wrongly set the key and object as well as you need to set the value to NSMutableDictionary. 您在这里的错误是您错误地设置了键和对象,以及需要将值设置为NSMutableDictionary。

If you want to edit the values in dictionary you must use NSMutableDictionary as it is mutable.Using NSDictionary you cant edit because it is immutable.You can't change anything. 如果要编辑字典中的值,则必须使用NSMutableDictionary变量,因为它是可变的。使用NSDictionary不能进行编辑,因为它是不可变的,您无法更改任何内容。

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@"Jan Month",@"Feb Month",@"Mar Month",@"Apr Month",@"May Month",@"June Month",@"July Month",@"Aug Month",@"Sep Month",@"Oct Month", nil];
NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];
NSMutableDictionary *testDetails=[NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];
[testDetails setValue:@"Fail" forKey:@"Feb Month"];

Now see the printed result 现在查看打印结果

Printing description of testDetails:
{
  "Apr Month" = Pass;
  "Aug Month" = Pass;
  "Feb Month" = Fail;
  "Jan Month" = Pass;
  "July Month" = Pass;
  "June Month" = Pass;
  "Mar Month" = Pass;
  "May Month" = Pass;
  "Oct Month" = Pass;
  "Sep Month" = Pass;
}

Also see the below screenshot 另请参阅以下屏幕截图

在此处输入图片说明

You should know to use the dictionary first. 您应该知道首先使用字典。

Apple NSDictionary API reference says Apple NSDictionary API参考说

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values . NSDictionary类为管理键和值的不可变关联的对象声明了编程接口。 Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key. 当您需要方便有效的方法来检索与任意键关联的数据时,请使用此类或其子类NSMutableDictionary。 NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. NSDictionary创建静态字典,而NSMutableDictionary创建动态字典。 (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.) (为方便起见,术语字典是指这些类之一的任何实例,但未指定其确切的类成员。)

Apple NSMutableDictionary API reference says Apple NSMutableDictionary API参考说

The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values . NSMutableDictionary类为管理键和值的可变关联的对象声明了编程接口。 It adds modification operations to the basic operations it inherits from NSDictionary. 它将修改操作添加到它从NSDictionary继承的基本操作中。

NSDictionary vs NSMutableDictionary NSDictionary vs NSMutableDictionary

Because from your code your NSDictionary testDetails will have only one value. 因为从您的代码中,您的NSDictionary testDetails将只有一个值。

 testDetails:
{
    Pass = "Jan Month";
}

you need to add your array to dictionary key and value like this: 您需要将数组添加到字典键和值,如下所示:

 NSMutableDictionary *testDetails = [NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];

then replace the key value using this: 然后使用以下命令替换键值:

[testDetails setValue:@"Fail" forKey:@"Feb Month"];

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

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