将首选项存储在 NSUser Defaults 中以便稍后调用
一直在致力于一些代码简化,并意识到如果我的应用程序有一个首选项系统将会非常有帮助。
现在这是我的代码的工作原理。
方法基于存储在 NSUserDefaults 中的整数运行,
例如
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scifi1"] == 040){
[self spaceDown];
}
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scifi1"] == 10040){
[self ctrldown];
[self spaceDown];
}
现在我想做的是,当我退出视图(通过特定按钮)时,将 @"scifi1" 的值转储到新的首选项中,例如 - 名为 @"savedscifi1" 的整数
现在我知道如何保存整数到 NSUserDefaults 中,
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:VALUEHERE forKey:@"savedscifi1"];
[userDefaults synchronize];
但是 - 我不确定如何替换 scifi1 的值而不是(在本例中)“VALUEHERE” - 任何人都可以帮忙解决这个问题吗?我觉得这真的很简单,但我忍不住觉得我有点厚……睡眠不足,而且临近最后期限!我知道我不能只打电话给@“scifi1”,但除此之外......?
Been working on some code streamlining and have realised that it would be really helpful if my app had a preferences system.
Now here's how my code works.
A method runs based upon an integer stored in NSUserDefaults
e.g.
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scifi1"] == 040){
[self spaceDown];
}
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scifi1"] == 10040){
[self ctrldown];
[self spaceDown];
}
Now what I want to do is when I exit the view (via a specific button) is to dump the value of @"scifi1" into a new preference, say for example - an integer named @"savedscifi1"
Now I know how to save integers into NSUserDefaults,
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:VALUEHERE forKey:@"savedscifi1"];
[userDefaults synchronize];
However - I'm not sure how I can substiture in the value of scifi1 instead of (in this case) 'VALUEHERE' - can anyone help with this? I feel it's really simple but I can't help but think I'm being a bit thick...sleep deprived and approaching a deadline! I know I can't just call up @"scifi1"but beyond that....??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)