OGeek|极客世界-中国程序员成长平台

标题: ios - setvalue :forkey: not working, 应用程序崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:42
标题: ios - setvalue :forkey: not working, 应用程序崩溃

我正在开发一个应用程序。我尝试使用以下代码为 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"ass",@"ass",@"ass",@"ass",@"ass",@"ass",@"ass",@"ass",@"ass",@"ass", 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.'

我尝试通过检查来解决这个问题,

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

请帮我解决这个问题。



Best Answer-推荐答案


您需要为对象设置testResult,为键设置testArray。现在如果你想改变它的值你需要将它声明为NSMutableDictionary

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

关于ios - setvalue :forkey: not working, 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978813/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4