检索保存的(NSUserDefaults)日期到另一个类
[[NSUserDefaults standardUserDefaults] setObject:datePicker.date forKey:@"birthDate"];
[[NSUserDefaults standardUserDefaults] setInteger:sexSegmented.selectedSegmentIndex forKey:@"sex"];
[[NSUserDefaults standardUserDefaults] synchronize];
这是我保存到 userdefaults 的值,并且是我想从另一个类中调用它们的值。
NSDate *birthDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"birthDate"];
NSInteger a=[[NSUserDefaults standardUserDefaults] integerForKey:@"sex"];
NSLog(@"%@",birthDate);
[datePicker setDate:birthDate animated:YES];
sexSegmented.selectedSegmentIndex=a;
这是我获取保存值的另一个类,但我在 birthDate 中看不到任何值。我想接受它并将新的 datePicker 设置为 birthDate。有人可以帮助我吗?
[[NSUserDefaults standardUserDefaults] setObject:datePicker.date forKey:@"birthDate"];
[[NSUserDefaults standardUserDefaults] setInteger:sexSegmented.selectedSegmentIndex forKey:@"sex"];
[[NSUserDefaults standardUserDefaults] synchronize];
This is the value that I am saving to userdefaults and from another class I want to call them.
NSDate *birthDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"birthDate"];
NSInteger a=[[NSUserDefaults standardUserDefaults] integerForKey:@"sex"];
NSLog(@"%@",birthDate);
[datePicker setDate:birthDate animated:YES];
sexSegmented.selectedSegmentIndex=a;
This is the other class from where I am getting the saved values, but I can't see any value in birthDate. I want to take it and set the new datePicker to birthDate. Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很有可能是
datePicker
的接口插座没有连接好。这将使datePicker
nil
并最终将nil
存储在用户默认值中。您可以通过
在第一个代码片段之前执行此操作来验证这一点。
It's most likely a case of the interface outlet of
datePicker
not being connected properly. This would makedatePicker
nil
and as a result you would end up storingnil
in the user defaults.You can validate this by doing
before your first snippet of code.