【发布时间】:2014-03-01 22:17:26
【问题描述】:
我正在尝试找出正确的数据结构来保存 'date' => 'value' 的键值
我正在尝试使用 NSMutableDictionary,因为每次用户输入一个值并按“保存”时它都必须保存。
但是,尝试 NSLog 字典的值始终为空。
- (IBAction)saveWeight:(id)sender
{
NSMutableDictionary *weightLog = [[NSMutableDictionary alloc] init];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *date = @"12/12/14";
[weightLog setObject:self.weightInput.text forKey:date];
NSLog(@"neweight you entered is %@", self.weightInput.text);
NSLog(@"weightlog is %@", weightLog);
[userDefaults setObject:weightLog forKey:@"Weightlog"];
[userDefaults synchronize];
}
日志输出:
diet[35744:70b] neweight you entered is 13
2014-03-01 16:25:58.712 diet[35744:70b] weightlog is {
"12/12" = 13;
}
【问题讨论】:
-
尝试打印您的 [weightLog objectForKey:@"yourkey"] 并查看打印的内容
-
那是你的实际代码吗?显示你得到的日志文本。
-
是的,这是我的实际代码。发生的情况是无论我输入多少个值,都只会打印出最后一个值。用日志输出编辑。
标签: ios nsuserdefaults nsmutabledictionary