Ios setvalue:forkey:不工作,应用程序正在崩溃

Ios setvalue:forkey:不工作,应用程序正在崩溃,ios,objective-c,xcode,nsdictionary,Ios,Objective C,Xcode,Nsdictionary,我正在开发一个应用程序。我尝试使用以下代码为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]; NSMutable

我正在开发一个应用程序。我尝试使用以下代码为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"];
我的应用程序在第四行出现错误时崩溃

*** 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.'
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥月份的密钥值编码。”
我试图通过检查来解决这个问题

po [self.testDetails valueForKey:"Feb Month"]
<extracting data from value failed>
po[self.testDetails valueForKey:“二月-月”]

请帮我解决这个问题。

因为从您的代码中,您的NSDictionary testDetails只有一个值

 testDetails:
{
    Pass = "Jan Month";
}
您需要将数组添加到字典键和值,如下所示:

 NSMutableDictionary *testDetails = [NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];
然后使用以下命令替换键值:

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

首先,您需要为对象设置
testResult
,为键设置
testArray
。现在,如果要更改它的值,需要将其声明为
NSMutableDictionary

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

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

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

您是否正确设置了对象和关键点?您是否正确使用了NSDictionary和NSMutableDictionary

我先检查了你下面的代码

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"];
现在查看结果。只有1个键值对

这里的错误是错误地设置了键和对象,并且需要将值设置为NSMutableDictionary

如果要编辑字典中的值,必须使用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"];
现在查看打印结果

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;
}
另请参见下面的屏幕截图

你应该先知道如何使用字典

NSDictionary类向管理键和值的不可变关联的对象声明编程接口。当您需要一种方便高效的方法来检索与任意键关联的数据时,请使用此类或其子类NSMutableDictionary。NSDictionary创建静态字典,NSMutableDictionary创建动态字典。(为了方便起见,术语dictionary指的是这些类之一的任何实例,而不指定其确切的类成员身份。)

NSMutableDictionary类向管理键和值的可变关联的对象声明编程接口它将修改操作添加到从NSDictionary继承的基本操作中


请尝试使用“无处不在”而不是“不要使用
valueForKey/setValue:forKey
,除非您可以解释为什么明确需要KVC。专用方法是
objectForKey/setObject:forKey
或–更可取–键订阅。您从另一个方向获得了键/值。您可能是指
[NSDictionary Dictionary WithObjects:testResult forKeys:testArray];
(首先是值,然后是键)…当然,正如@Nirav所说,您需要一个可变dict…在代码中用NSMutableDictionary替换NSDictionary。