全部,
我确定我忽略了一些东西,但我盯着这段代码太久了,试图弄清楚发生了什么。
-(IBAction)continue:(id)sender
{
//setters for the limits
NSLog(@"Log ageUnder18: %@", ageUnder18.text); //returns Y
NSMutableDictionary *cardLimits;
[cardLimits setObject:ageUnder18.text forKey:@"ageUnder18"];
NSLog(@"Just set %@", [cardLimits objectForKey:@"ageUnder18"]); //returns NULL
//more code here
}
为什么返回 NULL?
提前致谢, 詹姆斯
最佳答案
您没有初始化您的变量。事实上,您很幸运没有在 -setObject:forKey:
行上完全崩溃。您的 cardLimits
变量当前持有垃圾内存,即变量占用地址处堆栈中的任何内容。你需要使用
NSMutableDictionary *cardLimits = [NSMutableDictionary dictionary];
请注意,静态分析器应该能够为您捕捉到这一点。
关于iphone - NSMutableDictionary 不设置键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7534287/