iphone - NSMutableDictionary 不设置键

标签 iphone objective-c ios xcode

全部,

我确定我忽略了一些东西,但我盯着这段代码太久了,试图弄清楚发生了什么。

-(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/

相关文章:

iphone - 为什么 UITabBarController 中的 viewDidAppear 在 View 出现之前执行?

iphone - 复制到通用粘贴板

ios - 如何在 ios 的 ftp 服务器上上传 .png 或 .jpg 图像?

objective-c - NSURLRequest 在重定向请求时丢失 HTTP header “Authorization”

ios - Swift 希望将#selector 的参数暴露给 Objective-C

iphone - 在 Objective-C 中将 BOOL 转换为 id

ios - 无法点击 UITableView 中的最后一个单元格(仅在模拟器 4 中)

ios - Xcode 7.1 beta 2 - 禁用 ATS

iphone - iOS : How can I rotate a view control when another view segues to it?

android - 你能从应用商店下载的 Cordova 应用程序中提取 www 文件夹吗